0

Possible Duplicate:
Javascript: setInterval() behaviour with 0 milliseconds

I simply want to hear, if it is a problem having a setInterval with time set to 0 milliseconds? Will there be any performance issues? And will it be better to have e.g. 1000 milliseconds?

I have this script:

var countdown_id = setInterval(function() {
    var now = (new Date()).getTime()-(2*60*60*1000);
    ;
    $('.product').each(function() {
        var elm = $(this).attr('id');
        var prod_id = $("#"+elm+" #hidden-"+elm).val();


        var expires = $('#date-end-' + prod_id).val()*1000;

        var seconds_remaining = Math.round((expires - now)/1000);

        var hours = FormatNumberLength(Math.floor(seconds_remaining/(60*60)), 2);
        var sec_remaining = seconds_remaining-(hours*60*60);
        var minutes = FormatNumberLength(Math.floor(sec_remaining/60), 2);
        var sec_remaining2 = sec_remaining-(minutes*60);
        var seconds = FormatNumberLength(sec_remaining2, 2);

        var timestr = hours + ":" + minutes + ":"+seconds;

        if (expires) {
            if (seconds_remaining > 0) {
                $('#'+elm+' .time_left div').text(timestr);
            }
            else {
                $(this).hide();
            }
        }
        $('#'+elm+' .time_left div').text(timestr);
    });
}, 0);

Thanks in advance!

Community
  • 1
  • 1
denlau
  • 916
  • 2
  • 9
  • 21
  • The browser will effectively treat any delay less than some minimum as that minimum, but updating the DOM that fast is going to really tax the browser. – Pointy Sep 23 '12 at 17:31
  • You have really too much DOM-selector code to run this all the time; use caching variables and a proper timer instead of a zero-time-interval – Bergi Sep 23 '12 at 17:45

2 Answers2

1

if it is a problem having a setInterval with time set to 0 milliseconds?

You can't set 0 as the interval, as the browser will ignore it.

Will there be any performance issues? And will it be better to have e.g. 1000 milliseconds?

Of course 1000 milliseconds is better than 10(not zero) if it's possible.
Doing a thing every 10 ms costs you more than doing that exact thing only once 1000 ms.

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • What do you mean by "ignore it"? – lqc Sep 23 '12 at 17:34
  • 1
    @lqc. The browser will use the minimal interval which they defined. zero isn't an option... – gdoron Sep 23 '12 at 17:40
  • Well, okay.. But my script works when set to 0.. but will it be hard to older computers? I mean will it slow them? :-) – denlau Sep 23 '12 at 18:58
  • @denlau. It depends on what you do in your script... – gdoron Sep 23 '12 at 19:10
  • Well in the future i am going to check up on some products and hide them if = 0 and else check up on them with an ajax-call if the price is needed to be changed. Is that to harsh? It is because my timer in javascript is in miliseconds and in the database is in seconds, so the first time it updates time is ticking down 2 seconds instead of one because of the more accurate seconds in the javascript. – denlau Sep 23 '12 at 19:40
  • The browser does not ignore a 0ms interval. It will execute the function in the following code execution context. – Ben Feb 12 '14 at 03:48
  • @Benjammin', _"delay is the number of milliseconds (thousandths of a second) that the setInterval() function should wait before each call to func. As with setTimeout, there is a minimum delay enforced."_ [MDN](https://developer.mozilla.org/en/docs/Web/API/window.setInterval) – gdoron Feb 12 '14 at 07:59
  • Of course, my apologies, I was thinking of the setTimeout function. – Ben Feb 12 '14 at 08:13
  • @Benjammin', no harm done, but even in that case the browser won't let you use lower values than its minimum threshold, read the above quote again please, _"As with setTimeout,"_. – gdoron Feb 12 '14 at 08:18
1

Javascript is not multithreaded, it just simulates it with delayed execution. When the timer reaches your timeout interval, it will pause all other execution and run the code you told it to. Because of this, you might want to be careful about how short your interval is. It will probably tank your browser if you aren't careful.

monitorjbl
  • 4,280
  • 3
  • 36
  • 45
  • 3
    "it will pause all other execution and run the code you told it to" - not really. A timeout is just another browser event. When the timeout expires, an event is scheduled, but it doesn't mean it will be handled *now*. The browser is also free to extend the time as it wishes. – lqc Sep 23 '12 at 17:38
  • I didn't want to get into the event queue to answer this question, but you are correct. – monitorjbl Sep 23 '12 at 17:40