Skip to content

Instantly share code, notes, and snippets.

@Constellation
Created February 25, 2011 16:17
Show Gist options
  • Save Constellation/844020 to your computer and use it in GitHub Desktop.
Save Constellation/844020 to your computer and use it in GitHub Desktop.
// interval.js (c)id:Constellation
// provide intervalMap and intervalEach
// require jquery-1.5.1.js and underscore.js
// see also https://gist.github.com/814121
//
// usage
//
// // each
// intervalEach(
// _.range(200),
// function(elm) {
// for (var i = 0; i < 10000000;++i); // spin
// console.log(new Date().getTime(), "each");
// },
// {
// execTime: 100,
// interval: 3000
// })
// .done(function() {
// console.log("EACH DONE");
// });
//
// // map
// intervalMap(
// _.range(200),
// function(elm) {
// for (var i = 0; i < 10000000;++i);
// console.log(new Date().getTime(), "map");
// return elm * elm;
// })
// .done(function(res) {
// console.log("MAP DONE", res);
// });
//
// // nested each
// intervalEach(
// _.range(200),
// function(elm) {
// for (var i = 0; i < 10000000;++i);
// console.log(new Date().getTime(), "nested each");
// return intervalMap(
// _.range(1000),
// function(elm) {
// console.log(elm);
// },
// {
// execTime: 0,
// interval: 500
// });
// },
// {
// execTime: 100,
// interval: 3000
// })
// .done(function() {
// console.log("NESTED EACH DONE");
// });
//
// // nested map
// intervalMap(
// _.range(200),
// function(elm) {
// for (var i = 0; i < 10000000;++i);
// console.log(new Date().getTime(), "nested map");
// return intervalMap(
// _.range(200),
// function(elm2) {
// return elm * elm2;
// });
// })
// .done(function(res) {
// console.log("NESTED MAP DONE", res);
// });
(function intervalDefine(global) {
var getNow = (function() {
if (Date.now) {
return Date.now;
} else {
return function getNow() {
return new Date().getTime();
}
}
})();
function intervalMap(list, func, opt) {
var d = $.Deferred();
var len = list.length;
var now = getNow();
var option = opt || {};
var execTime = option.execTime || 1000;
var interval = option.interval || 100;
var i = 0;
var result = [];
function aggregater(elm) {
result[i] = elm;
++i;
if (getNow() - now > execTime && i < len) {
now = getNow();
_.delay(nextElement, interval);
} else {
nextElement();
}
}
function nextElement() {
while (i < len) {
var res = func.call(list, list[i], i, list);
// checker for deferred. described in jQuery.when
if (res && $.isFunction(res.promise)) {
res.promise().done(aggregater);
return;
}
result[i] = res; // map result
++i;
if (getNow() - now > execTime && i < len) {
now = getNow();
_.delay(nextElement, interval);
return;
}
}
d.resolve(result);
}
_.delay(nextElement, 0);
return d.promise();
}
function intervalEach(list, func, opt) {
var d = $.Deferred();
var len = list.length;
var now = getNow();
var option = opt || {};
var execTime = option.execTime || 1000;
var interval = option.interval || 100;
var i = 0;
function aggregater() {
++i;
if (getNow() - now > execTime && i < len) {
now = getNow();
_.delay(nextElement, interval);
} else {
nextElement();
}
}
function nextElement() {
while (i < len) {
var res = func.call(list, list[i], i, list);
// checker for deferred. described in jQuery.when
if (res && $.isFunction(res.promise)) {
res.promise().done(aggregater);
return;
}
++i;
if (getNow() - now > execTime && i < len) {
now = getNow();
_.delay(nextElement, interval);
return;
}
}
d.resolve();
}
_.delay(nextElement, 0);
return d.promise();
}
// not nestable
function intervalMapNoNest(list, func, opt) {
var d = $.Deferred();
var len = list.length;
var now;
var option = opt || {};
var execTime = option.execTime || 1000;
var interval = option.interval || 100;
var start = 0;
var result = [];
function registerNext() {
now = getNow();
var i = start;
while (i < len) {
// at least treats 10 element
for (var max = (i + 10 < len ? i + 10 : len);i < max; ++i) {
result[i] = func.call(list, list[i], i, list);
}
if (getNow() - now > execTime && i < len) {
now = getNow();
start = i;
_.delay(registerNext, interval);
return;
}
}
d.resolve(result);
}
_.delay(registerNext, 0);
return d.promise();
}
// not nestable
function intervalEachNoNest(list, func, opt) {
var d = $.Deferred();
var now;
var len = list.length;
var option = opt || {};
var execTime = option.execTime || 1000;
var interval = option.interval || 100;
var start = 0;
function registerNext() {
now = getNow();
var i = start;
while (i < len) {
// at least treats 10 element
for (var max = (i + 10 < len ? i + 10 : len);i < max; ++i) {
func.call(list, list[i], i, list);
}
if (getNow() - now > execTime && i < len) {
now = getNow();
start = i;
_.delay(registerNext, interval);
return;
}
}
d.resolve();
}
_.delay(registerNext, 0);
return d.promise();
}
// export
this.intervalMap = intervalMap;
this.intervalEach = intervalEach;
// this.intervalMapNoNest = intervalMapNoNest;
// this.intervalEachNoNest = intervalEachNoNest;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment