how many javascript setTimeout/ setInterval call can be set simultaneously in one page?

前端 未结 4 507
别那么骄傲
别那么骄傲 2020-12-01 07:45

I have to use atleast 2 setTimeouts and 1 setInterval. Does this have any dependency on the browser or javascript engine being used?

4条回答
  •  广开言路
    2020-12-01 08:25

    On a page you can have as many setTimeouts/setIntervals running at once as you wish, however in order to control each individually you will need to assign them to a variable.

    var interval_1 = setInterval("callFunc1();",2000);
    var interval_2 = setInterval("callFunc2();",1000);
    clearInterval(interval_1);
    

    The same code above applies to setTimeout, simply replacing the wording.

    As Kevin has stated, JavaScript is indeed single threaded, so while you can have multiple timers ticking at once, only one can fire at any one time - i.e. if you have one that fires a function which 'halts' in execution, for example with an alert box, then that JS must be 'resumed' before another can trigger I believe.

    One further example is given below. While the markup is not valid, it shows how timeouts work.

    
        
            
        
    
    

提交回复
热议问题