user4065758
user4065758

Reputation: 319

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)

Upvotes: 2

Views: 7085

Answers (2)

Trinimon
Trinimon

Reputation: 13967

May be this one works for you:

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

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

setTimeout(someprogram, 1000);

Upvotes: 2

StateLess
StateLess

Reputation: 5402

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);

Upvotes: 5

Related Questions