Giter Club home page Giter Club logo

checknet's Introduction

Checknet

Simple JavaScript library which periodically checks internet connection and calls functions of your choice when the connection is dropped or restored. I'm very interested in feature requests so if you have one, please open an issue here: https://github.com/tdriley/checknet/issues

Setup

Include the checknet.min.js (found in the src/ folder) on each page you want to be able to detect internet connection on:

<script src="checknet.min.js"></script>

Usage

Somewhere after including the script, add your event listeners for 'dropped' and 'restored' to the Checknet object, passing in a function you want to call in each case:

Checknet.addEventListener('dropped', function(){
	//what should happen when the connection drops.
	console.log('dropped');
});

Checknet.addEventListener('restored', function(){
	//what should happpen when a dropped connection resumes.
	console.log('restored');
});

...and then set the check running by calling the start() method:

Checknet.start();

Note: The page you are running the code on must be served from the internet (running it locally will not detect dropped internet connection).

Methods for further usage

.set(name, value)

Takes two required params 'name' (string) and value (mixed types):

Checknet.set('checkInterval', 5000); 											//sets the interval in milliseconds between each check (default 3000, minimum 1000).
Checknet.set('checkUrls', ['http://asite.com', 'https://anothersite.co.uk']); 	//sets the array of URLs to check connection with.

Note on 'checkUrls': By default Checknet will use the current page URL. Any domains you add which are not the same as the one on which the code is r must be able to accept CORS requests (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) from the domain you are running the code on. Adding a new array will overwrite the existing one. The array can contain only one URL if you wish.

.getStatus()

Returns a JS object containing useful info:

{
	conActive: true, 											//true if connection is active on last check, false if not.
	checkUrls: ['http://someurl.com', 'https:anotherurl.com'], 	//the current array of URLs which is being used to check there is a connection.
	checkInterval: 3000 										//the current interval in milliseconds between each check (default 3000).
}

Building locally

If you want to modify the src/ files locally, clone or download the repo, navigate inside the 'checknet' folder and use npm to install the node modules required for building:

sudo npm install

...and then just type 'grunt' to build:

grunt

checknet's People

Stargazers

nerdyamin avatar Omid Gharib avatar

Watchers

James Cloos avatar Tom Riley avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.