Code Monkey home page Code Monkey logo

isfastnet's Introduction

Built on top of this Stack Overflow answer.

Version 2

Breaking change

A default export has been added now to ensure that the package can be integrated smoothly when using it with ES6 imports. Example 3 has been added to indicate the same

Examples

In the browser

<script type="module">
    import isFastNet from 'https://cdn.jsdelivr.net/npm/[email protected]'
    isFastNet((value) => {
    let speed = value ? 'fast':'slow';
    console.log('Internet is ' + speed);
})
</script>

As an ES6 import

<script>
import isFastNet from 'isFastNet'
isFastNet((data) => {
    /**
    * data = {
    *   "isFast": Boolean,
    *   "averageLatency": Number,
    *   "threshold": Number,
    *   "latencyReadings": [Number]
    * } 
    */
    console.log('Received latency data: ' + JSON.stringify(data));
}, { verbose: true })
</script>

With verbose option

<script type="module">
    import isFastNet from 'https://cdn.jsdelivr.net/npm/[email protected]'
    isFastNet((data) => {
        /**
        * data = {
        *   "isFast": Boolean,
        *   "averageLatency": Number,
        *   "threshold": Number,
        *   "latencyReadings": [Number]
        * } 
        */
        console.log('Received latency data: ' + JSON.stringify(data));
    }, { verbose: true })
</script>

Using with nuxt.js

Do a normal ES6 import

import isFastNet from 'isFastNet'

Add isfastnet to the transpile option in nuxt.config.js

...
build: {
    transpile: ['isfastnet']
}
...

Usage

isFastNet((value) => {
    if(value){
        // Internet is fast
    }
    else{
        // Internet is slow
    }
}, { 
timesToTest: 5, // optional, number of times to load the image default is 5
threshold: 200, // optional, threshold in ms after which internet speed is considered slow
image:  "http://www.google.com/images/phd/px.gif", //  optional, url of the tiny image to load, keep this on a CDN
allowEarlyExit: true, // optional, if the first request takes greater than threshold*3 ms then the function exits with false
verbose: false  // optional, if set, it returns an object with all calculated latency data. Overrides allowEarlyExit option (See Example "With verbose option" for usage)
})

isfastnet's People

Contributors

saunved avatar qbunt 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.