var td = {
setTimeout: function(callback, ms){
var dt = new Date();
var i = dt.getTime(), future = i+ms;
while(i<=future){
if(i === future){
callback();
}
// i = dt.getTime();
i++;
}
}
};
td.setTimeout(function(){
console.log("Hello"); // executes immediately
}, 3000);
The problem with this approach is that, at the end of the while block I am setting the value of i
to i + 1
which I think, should be set to the current timestamp (dt.getTime()
), that way, future
(a timestamp value at which the callback function should execute) can be compared with the current timestamp, which is the value of i
.
But when I comment out i++
and uncomment this line:
i = dt.getTime();
and run the script, the browser freezes. Is there a way to create a setTimout()
method that would serve the same purpose as window.setTimeout()
?