Revision: 62317
Updated Code
at February 16, 2013 03:32 by McSodbrenner
Updated Code
var setExactTimeout = function(callback, duration, resolution) { var start = (new Date()).getTime(); var timeout = setInterval(function(){ if ((new Date()).getTime() - start > duration) { callback(); clearInterval(timeout); } }, resolution); return timeout; }; var clearExactTimeout = function(timeout) { clearInterval(timeout); };
Revision: 62316
Updated Code
at February 16, 2013 03:32 by McSodbrenner
Updated Code
var setExactTimeout = function(callback, duration, resolution) { var start = (new Date()).getTime(); var timeout = setInterval(function(){ console.log(duration); if ((new Date()).getTime() - start > duration) { callback(); clearInterval(timeout); } }, resolution); return timeout; }; var clearExactTimeout = function(timeout) { clearInterval(timeout); };
Revision: 62315
Updated Code
at February 16, 2013 03:04 by McSodbrenner
Updated Code
var setExactTimeout = function(callback, duration, resolution) { var start = (new Date()).getTime(); var timeout; timeout = setInterval(function(){ if ((new Date()).getTime() - start > duration) { callback(); clearInterval(timeout); } }, resolution); return timeout; }; var clearExactTimeout = function(timeout) { clearInterval(timeout); };
Revision: 62314
Updated Code
at February 16, 2013 03:01 by McSodbrenner
Updated Code
var setExactTimeout = function(callback, duration, resolution) { var start = (new Date()).getTime(); var timeout; timeout = setInterval(function(){ if ((new Date()).getTime() - start > duration) { callback(); clearInterval(timeout); } }, resolution); return timeout; }; var clearExactTimeout = function(timeout) { clearInterval(timeout); }; var clearExactTimeout = function(timeout) { clearInterval(timeout); };
Revision: 62313
Updated Code
at February 16, 2013 02:54 by McSodbrenner
Updated Code
// more precise than setTimeout // resolution: how often the realtime is checked in ms var setExactTimeout = function(callback, duration, resolution) { var start = (new Date()).getTime(); var timeout; timeout = setInterval(function(){ console.log(duration); if ((new Date()).getTime() - start > duration) { callback(); clearInterval(timeout); } }, resolution); return timeout; }; var clearExactTimeout = function(timeout) { clearInterval(timeout); };
Revision: 62312
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 15, 2013 19:36 by McSodbrenner
Initial Code
// more precise than setTimeout // resolution: how often the realtime is checked in ms var setExactTimeout = function(callback, duration, resolution) { var start = (new Date()).getTime(); var timeout; var loop = function(){ if ((new Date()).getTime() - start > duration) { callback(); return; } timeout = setTimeout(function(){ loop(); }, resolution); }; loop(); return timeout; };
Initial URL
Initial Description
This is a more precise version of the native setTimeout(). It uses the same parameters as setTimeout, but adds a third parameter "resolution" which defines how often (in ms) to check for the time that passed. // alert after 5 seconds with an inaccuracy of 20 milliseconds var timeout = setExactTimeout(function(){ alert('done'); }, 5000, 20); // comment out to show "done" clearExactTimeout(timeout);
Initial Title
Exact Javascript Timeout
Initial Tags
javascript
Initial Language
JavaScript