Using setInterval() to do simplistic continuous polling

后端 未结 5 640
孤街浪徒
孤街浪徒 2020-12-13 03:45

For a simple webapp that needs to refresh parts of data presented to the user in set intervals, are there any downsides to just using setInterval() to get a JSON from an end

5条回答
  •  孤城傲影
    2020-12-13 04:16

    You can do just like this:

    var i = 0, loop_length = 50, loop_speed = 100;
    
    function loop(){
        i+= 1; 
        /* Here is your code. Balabala...*/
        if (i===loop_length) clearInterval(handler);
    }
    
    var handler = setInterval(loop, loop_speed);
    

提交回复
热议问题