Skip to content

Instantly share code, notes, and snippets.

@h2non
Created February 20, 2013 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h2non/4994163 to your computer and use it in GitHub Desktop.
Save h2non/4994163 to your computer and use it in GitHub Desktop.
Simple experimental wrapper for setInterval/clearInterval native JavaScript Functions
<html>
<head>
<title>setInterval/clearInterval Wrapper</title>
<script type="text/javascript">
/**
* Simple experimental wrapper for setInterval/clearInterval native JavaScript Functions
* @version 0.1 Alpha
* @author Tomas Aparicio
* @license WTFPL
*/
if (!window.setInterval.isWrapped) {
(function(w){
var wrap,
intervalsCheckerId,
// copy native Functions
__nativeSI__ = w.setInterval,
__nativeCI__ = w.clearInterval;
// compile-time Functions
function remove(arr, from, to) {
var rest = arr.slice((to || from) + 1 || arr.length);
arr.length = from < 0 ? arr.length + from : from;
arr.push.apply(arr, rest);
return arr;
}
function stopIntervalCheck() {
wrap.started = false;
__nativeCI__(intervalsCheckerId);
intervalsCheckerId = false;
}
function startIntervalCheck() {
if (!wrap.started) {
wrap.started = true;
intervalsCheckerId = __nativeSI__(intervalCheckFn, wrap.checkTimeInterval);
}
return intervalsCheckerId;
}
function intervalCheckFn() {
var func, i;
for (i=0;i<wrap.store.length;i+=1) {
if (wrap.store[i][1] === true) {
func = wrap.store[i][2][0];
if (func && ((new Date().getTime() - func.__lastCall__) > wrap.maxTimeout) || func.__callCounter__ >= wrap.maxCallCounter) {
__nativeCI__(wrap.store[i][0]);
wrap.store = remove(wrap.store, i);
}
}
}
if (wrap.store.length === 0) {
stopIntervalCheck();
}
}
// wrap global clearInterval native Function (interval id or Function)
w.clearInterval = function (id) {
for (var i=0;i<wrap.store.length;i+=1) {
if (typeof id === 'function' && wrap.store[i][2][0] === id) {
__nativeCI__(wrap.store[i][0]);
wrap.store = remove(wrap.store, i);
return true;
}
if (wrap.store[i][0] === id) {
__nativeCI__(id);
wrap.store = remove(wrap.store, i);
return true;
}
}
// clear non-store interval
return __nativeCI__(id);
};
// added clearAllIntervals global Function
w.clearAllIntervals = function (all) {
for (var i=0;i<wrap.store.length;i+=1) {
if (all) {
__nativeCI__(wrap.store[i][0]);
wrap.store = [];
} else {
if (wrap.store[i][1] === true) {
__nativeCI__(wrap.store[i][0]);
}
}
}
stopIntervalCheck();
};
// wrap global setInterval native Function
w.setInterval = wrap = function (fn, delay /*, argumentToPass1... */) {
var t, i, fstr, id,
check = true,
args = Array.prototype.slice.call(arguments, 2),
exists = false;
if (!(fn instanceof Function)) {
return false;
}
// check interval
for (i=0;i<wrap.store.length;i+=1) {
t = wrap.store[i][2];
if (t[0] === fn && t[1] === delay) {
exists = true;
break;
}
}
if (!exists) {
// especific Function Object properties
fn.__lastCall__ = new Date().getTime();
fn.__callCounter__ = 0;
// create interval
id = fn.__intervalId__ = __nativeSI__(function () {
fn.__lastCall__ = new Date().getTime();
fn.__callCounter__ += 1;
fn.apply(null, args);
}, delay);
fstr = fn.toString();
// discard min time
if (delay < wrap.minIntervalTime || fstr.match(/clearInterval/g) !== null) {
check = false;
}
// store it (multidimensional Array)
wrap.store.push([ id, check, arguments ]);
startIntervalCheck();
return id;
}
return false; // todo
};
/* setInterval static properties */
// store intervals
wrap.store = [];
wrap.checkTimeInterval = 5 * 1000;
wrap.maxTimeout = 15 * 1000;
wrap.isWrapped = true;
wrap.maxCallCounter = 3;
wrap.minIntervalTime = 1000;
wrap.started = false;
}(window));
}
</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript" src="http://backbonejs.org/backbone-min.js"></script>
<script type="text/javascript" src="https://raw.github.com/highslide-software/highcharts.com/master/js/highcharts.src.js"></script>
<script type="text/javascript">
// discard interval
setInterval(function(){
console.log('0 - Mi super interval persistente cada 500 ms!!');
}, 500);
var id = setInterval(function(){
console.log('1 - Mi super interval cada 3 segundos!!');
}, 3000);
setInterval(function(){
console.log('2 - Mi super interval cada 5 segundos!!');
}, 5000);
setInterval(function(){
console.log('3 - Mi super interval cada 10 segundos!!');
}, 10000);
setInterval(function(){
console.log('4 - Mi super interval cada 15 segundos!!');
}, 15000);
setInterval(function(){
console.log('5 - Mi super interval cada 20 segundos!!');
}, 20000);
setTimeout(function () {
clearInterval(id);
//clearAllIntervals();
}, 6000);
/*
setTimeout(function(){
setInterval.stopIntervalCheck();
setTimeout(function(){
setInterval.startIntervalCheck();
}, 1500);
}, 8000);
*/
</script>
</head>
<body>
<h1>setInterval/clearInterval Wrapper</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment