简体   繁体   中英

Execute after setTimeout finished

Probably not very suitable title, but the question is the following.

I've got this piece of code

var someprogram = function(){ if(stopprocess) return; //do things setTimeout(someprogram, 1000); } setTimeout(someprogram, 1000);

Sofar it works. After some number ot iterations, lets say 12, stopprocess is true and whole thing stops. I want to do some more things after the return. So the question is what is the best way to rewrite the structute to be able to do so? Because I need the timeout, but I don't know how else to stop the whole thing without using a return-statement. Any ideas? (I have only one javascript file for this task)

there are many ways to do this simple way is to write all your logic in a function and execute it.

    var logic = function(){
       //do things 
     }

and execute it before return

var someprogram = function(){
    if(stopprocess){ 
       logic(); // your code is executed now
       return;
   }

    setTimeout(someprogram, 1000);
}
setTimeout(someprogram, 1000);

May be this one works for you:

var someprogram = function() { 
    if(stopprocess) {
        //do the rest

    } else {
        //do things 
        setTimeout(someprogram, 1000); 
    }
} 

setTimeout(someprogram, 1000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM