Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

javascript - JS setInterval not starting right away but otherwise working well

Can someone tell me why using this setInterval takes 15 seconds to start running and how to make it run without delay? It works well in repeating every 15 seconds like it should but I want to remove the start

function pollServer() {
    intervalId = window.setInterval(function ()  {
        callE();
        callA();
        callC();
        callB();
        callD();
        
     }, 15000);  
    
 }
 pollServer();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To fix this, call the function before setting the interval like this:

function foo() {
    callE();
    callA();
    callC();
    callB();
    callD();
}
foo();
setInterval(foo, 15000);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...