Is calling setTimeout with a negative delay ok?

JavascriptSettimeout

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

According to the MDN reference, the specification requires that there is a minimum timeout.

If you provide something less than this (HTML5 spec says 4ms) then the browser will just ignore your delay and use the minimum.

So negatives should be fine, since it'll just be less than the minimum.


Apparently, this isn't always the case (isn't that always the way with web development!). According to ( http://programming.aiham.net/tag/browser-compatibility/ ):

> Providing setTimeout a negative time will not always result in the > callback function being called. This works in other browsers, but in > Internet Explorer (8 or lower) you have to make sure any negative > times are changed to zero.

I haven't tested this myself, but like Thomasz said, it's probably better to be safe.

Solution 2 - Javascript

Better be safe than sorry:

setTimeout(callback, Math.max(sleepTime, 0))

Solution 3 - Javascript

You could also use a conditional statement, like so:

if (sleepTime < 0) {
  sleepTime = 0;
}
setTimeout(callback, sleepTime);

Solution 4 - Javascript

Hmm... The solutions mentioned solves the problem at the call to setTimeout, so it needs to be written each time a call is made. Isn't it better to solve it directly in setTimeout?

// Run this once.
(function(){
	var oldSetTimeout = setTimeout
	setTimeout = function(callback, delay){
		return oldSetTimeout(callback, Math.max(delay, 0))
	}
})()

// Call setTimeout safely with a negative delay.
setTimeout(function(){ console.log("Hello World") }, -42)

Solution 5 - Javascript

Yes it is ok to use negative timeout in milli-seconds like this.

        setTimeout(() => {
        btn.style.top = '16.2%';
        btn.style.left = '35.8%';
        btn.style.transition = 'all 1.2s ease';
        },-1.2*1000);

This code will initiate transition on button of id btn after 1.2*1000 seconds of delay,style top and style left

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionripper234View Question on Stackoverflow
Solution 1 - JavascriptJonathon BolsterView Answer on Stackoverflow
Solution 2 - JavascriptTomasz NurkiewiczView Answer on Stackoverflow
Solution 3 - Javascriptdku.rajkumarView Answer on Stackoverflow
Solution 4 - JavascriptPeppe L-GView Answer on Stackoverflow
Solution 5 - JavascriptZia KhanView Answer on Stackoverflow