The following snippet sets a timeout that I'd like to last at least a second:
var currentTimeMillis = new Date().getTime();
// do stuff...
var sleepTime = 1000 - (new Date().getTime() - currentTimeMillis);
Given that sleepTime
can be a negative number, is it safe to call setTimeout
, like this:
setTimeout(callback, sleepTime)
Or do I need to check for negative values before calling setTimeout
?