Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript setTimeout doesn't allow interval under 100 milliseconds

I want to fire a recursive function every 10 milliseconds, but setTimeout doesn't seem to take any value under 100, no matter what I put.

decrementCountdown = function() {
    console.log('fired');
    t = setTimeout("decrementCountdown()", 100);
}

Why can't I set a value under 1000 milliseconds?

EDIT : Looks like my timer is firing correctly...the problem is when I use it to call a function ( like Jquery's html() ). Even though setTimeout is calling the function at correct intervals, it won't fire any faster than 100 milliseconds.

Anyone know how I can make a countdown timer on my page that counts down by 10 milliseconds?

like image 543
zakdances Avatar asked Feb 24 '26 14:02

zakdances


2 Answers

A better example is:

var stop = 0;
decrementCountdown = function() {
    console.log(new Date().getTime());
    stop++;
    if (stop<10) t = setTimeout("decrementCountdown()", 100);
}

decrementCountdown();

As you should see, the timeout is triggered about every 100ms (jsFiddle).

like image 177
j08691 Avatar answered Feb 26 '26 02:02

j08691


I tried your example and it seemed to work fine.

For what it's worth I'd save on the constant redeclaration of setTimeout by using

function decrementCountdown()
{
    /*Do your thing*/
}
setInterval(decrementCountdown, 100);
like image 26
CBusBus Avatar answered Feb 26 '26 02:02

CBusBus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!