forked from mtrpcic/polljs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll.min.js
1 lines (1 loc) · 1.87 KB
/
poll.min.js
1
var Poll={version:"0.3",start:function(e){if(typeof e==="string"){if(Poll.exists(e,true)){e=Poll.timers[e].config}else{throw"PollJS: The interval you are trying to re-activate does not exist."}}else{action=e.action;e.internal_action=e.action;e.action=function(){Poll.util.attempts(e.name,e.internal_action)}}if(e.start){if(e.interval){if(e.increment){Poll.timers[e.name]={type:"timeout",config:e,attempts:0,active:true,value:setTimeout(function(){Poll.util.timeout(e.name,e.action,e.interval)},e.start)}}else{Poll.timers[e.name]={type:"timeout",config:e,attempts:0,active:true,value:setTimeout(function(){e.action();Poll.timers[e.name].value=setInterval(e.action,e.interval);Poll.timers[e.name].type="interval"},e.start)}}}else{Poll.timers[e.name]={type:"timeout",config:e,attempts:0,active:true,value:setTimeout(e.action,e.start)}}}else if(e.interval){if(e.increment){Poll.timers[e.name]={type:"interval",config:e,attempts:0,active:true,value:setTimeout(function(){Poll.util.timeout(e.name,e.action,e.interval+e.increment)},e.interval)}}else{Poll.timers[e.name]={type:"interval",config:e,attempts:0,active:true,value:setInterval(e.action,e.interval)}}}else{throw"PollJS: You need to define a start, an interval, or both."}},util:{attempts:function(e,t){var n,r=Poll.timers[e];Poll.timers[e].attempts+=1;n=t();if(n===false){Poll.stop(e)}if(r.config.attempts){if(r.attempts==r.config.attempts){Poll.stop(e);r.config.fallback()}}},timeout:function(e,t,n){var r,i=Poll.timers[e].config;r=n+(i.increment||0);Poll.timers[e].value=setTimeout(function(){Poll.util.timeout(i.name,t,r)},r);Poll.timers[e].type="timeout";t()}},exists:function(e,t){if(Poll.timers[e]!==undefined&&(t===true||Poll.timers[e].active===true)){return true}else{return false}},stop:function(e){if(Poll.exists(e)){var t=Poll.timers[e];if(t.type=="interval"){clearInterval(t.value);t.active=false}else{clearTimeout(t.value);t.active=false}}},timers:{}}