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
returnsstring
"aborted" or "expired" as first argument
Examples
-
Check if
window.foo
exists, checking50
times every150ms
// 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"); });
-
Without attempts and interval - by default, an attempt of
999
times every500ms
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"); });
-
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"); });
-
An attempt can be aborted by setting it up as a variable and calling
abort()
methodvar sample = LoadAttempt(function(){ return (window.foo) ? true : false; }, function(){ alert("true"); }); // abort loadAttempt setTimeout(function(){ sample.abort(); }, 1000);
-
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);
-
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!"); });