setTimeout or setInterval?

前端 未结 19 3157
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 04:59

As far as I can tell, these two pieces of javascript behave the same way:

Option A:

function myTimeoutFunction()
{
    doStuff();
           


        
19条回答
  •  终归单人心
    2020-11-21 05:43

    I think SetInterval and SetTimeout are different. SetInterval executes the block according to the time set while, SetTimeout executes the block of code once.

    Try these set of codes after the timeout countdown seconds:

    setInterval(function(e){
        alert('Ugbana Kelvin');
    }, 2000);
    

    and then try

    setTimeout(function(e){
        alert('Ugbana Kelvin');
    }, 2000);
    

    You can see the differences for yourself.

提交回复
热议问题