1.0.2 • Published 7 years ago

online-js v1.0.2

Weekly downloads
38
License
MIT
Repository
github
Last release
7 years ago

Online-js

Checking internet connection.

Browser Support

ChromeFirefoxSafariOperaEdgeIE
Latest ✔Latest ✔Latest ✔Latest ✔Latest ✔8+ ✔

Browser Matrix

Installing

npm i online-js --save
# or
yarn add online-js

Usage

Basic

import Online from 'online-js'

const statusChecker = Online()

const callback = (status) => {
  if (status === true) {
    console.info('Connected!')
  } else {
    console.warn('Disconnected!')
  }
}

statusChecker.onUpdateStatus(callback)

Advanced

// Options with default values
const options = {
  url: 'favicon.ico',  // Link on your site. Cross-domain requests not supported
  delay: 2,            // Delay between checking in seconds
  startOnload: true    // Start checking after page load
}

const statusChecker = Online(options)

// Add listeners
statusChecker.onUpdateStatus(callback1, callback2, callback3...)

// Removing listeners
statusChecker.removeListener(callback)  // Single
statusChecker.removeAllListeners()      // All

// Methods
statusChecker.check(callback)  // Single checking
statusChecker.start()          // Start checking. Don't forget to add a listener
statusChecker.stop()           // Stop checking

Using with Vue.js

import Vue from 'vue'
import Online from 'online-js'

Vue.prototype.$online = Online({
  url: 'imgs/check-connect.jpg'
})

new Vue({
  created() {
    this.$online.onUpdateStatus(status => {
      alert(status ? 'Connected' : 'Disconnected')
    })
  }
})

License

MIT