Return to Snippet

Revision: 62317
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
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
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
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
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
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