Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wait for function done (with setInterval) and run next function

how to run next function after first done with setInterval?

for example:

step1();
step2();

setInterval(step1, 1000).done(function() { 
    setInterval(step2, 1000).done( /* next step */);
});

please help me with solution!

like image 379
bsbak Avatar asked Aug 29 '26 10:08

bsbak


1 Answers

Edit: This is an old answer. Now you can achieve this using promises also but the code will be slightly different.

If you don't want to use a promise you can use a simple flag to achieve such a thing. Please see example below:

var flag = true;

function step1() {
  console.log('title');
}

function step2() {
  console.log('subtitle');
}

function wrapper() {
  if(flag) {
    step1();
  } else {
    step2();
  }
  flag = !flag;
}

setInterval(wrapper, 30000);
like image 148
TeaCoder Avatar answered Sep 01 '26 01:09

TeaCoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!