Coder Social home page Coder Social logo

loadattempt.js's Introduction

LoadAttempt Script

Lightweight Promise-type script to attempt execution for any type of condition. You just need to return a boolean true.

Syntax

Anonymous

LoadAttempt([attempts:int], [interval:int], [check:function], [success:function], [expires|abort:function]);

Methods

var promise = LoadAttempt([attempts:int], [interval:int], [check:function]);

promise.success([success:function]);

promise.expires([expires|abort:function]);

attempt (optional)

  • The number of attempts to run the check function
  • NOTE: optional in conjuction with interval (see examples)

interval (optional)

  • Time in ms in between attempts
  • NOTE: optional in conjuction with attempt (see examples)

check

  • function for the promise script to verify
  • Must return a boolean true for success

success

  • function to execute on successful check

expires|abort (optional)

  • function to execute on expire/abort
  • the function returns string "aborted" or "expired" as first argument

Examples

  • Check if window.foo exists, checking 50 times every 150ms

    // anonymous
    LoadAttempt(50, 150, function(){
        return (window.foo) ? true : false;
    }, function(){
        alert("true");
    });
    
    // method
    var sample = LoadAttempt(50, 150, function(){
        return (window.foo) ? true : false;
    });
    
    sample.success(function(){
        alert("true");
    });

    JSFiddle Examples: Anonymous | Method

  • Without attempts and interval - by default, an attempt of 999 times every 500ms is set

    // anonymous
    LoadAttempt(function(){
        return (window.foo) ? true : false;
    }, function(){
        alert("true");
    });
    
    // method
    var sample = LoadAttempt(function(){
        return (window.foo) ? true : false;
    });
    
    sample.success(function(){
        alert("true");
    });

    JSFiddle Examples: Anonymous | Method

  • Optional attempts only, default interval

    // anonymouse
    LoadAttempt(50, function(){
        return (window.foo) ? true : false;
    }, function(){
        alert("true");
    });
    
    // method
    var sample = LoadAttempt(50, function(){
        return (window.foo) ? true : false;
    });
    
    sample.success(function(){
        alert("true");
    });

    JSFiddle Examples: Anonymous | Method

  • An attempt can be aborted by setting it up as a variable and calling abort() method

    var sample = LoadAttempt(function(){
        return (window.foo) ? true : false;
    }, function(){
        alert("true");
    });
    
    // abort loadAttempt
    setTimeout(function(){
        sample.abort();
    }, 1000);

    JSFiddle Examples: Anonymous | Method

  • If you have an expires/abort function listener, you can listen for that event

    var sample = LoadAttempt(function(){
        return (window.foo) ? true : false;
    });
    
    sample.success(function(){
        alert("true");
    });
    
    sample.expires(function(type){
        alert((type === "aborted") ? "attempt aborted!" : "all attempts expired!");
    });
    
    // abort loadAttempt
    setTimeout(function(){
        sample.abort();
    }, 1000);

    JSFiddle Examples: Anonymous | Method

  • If attempts expires

    var sample = LoadAttempt(15, 150, function(){
        return (window.foo) ? true : false;
    });
    
    sample.expires(function(type){
        alert((type === "aborted") ? "attempt aborted!" : "all attempts expired!");
    });

    JSFiddle Examples: Anonymous | Method

loadattempt.js's People

Contributors

mitzerh avatar

Stargazers

Michael Canlas avatar  avatar

Watchers

James Cloos 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.