Javascript: calling a function with a delay inside another deferred function

So, I have a delayed function called delayed1, which is repeated every time cond1 is true, the problem is that do_something1 () is called inside this delayed1 function, and if cond3 is true, it calls another delayed function called delayed2. Now the problem is that I want delayed1 to wait for delayed2 to complete (which means until cond2 is false) and then resume again, which does not seem to happen. As soon as delayed2 starts, delayed1 does not wait for completion and resumption. Is there any way to do this? Hope I was clear enough ...

function delayed2() {
    setTimeout(function () {
       do_something2();
        if ( cond2 ) {
            delayed2();
        }
    }, 1000)
}

function do_something1(){
    if ( cond3 ){
        delayed2();
    }
    else {
        something_else();
    }
}

function delayed1() {
    does_something_here();
    setTimeout(function () {
        do_something1();
        if ( cond1 ){
            delayed1();
        }
    }, 1000)

}
+4
3

. :

var count = 0;

function delay1(callback) {
  setTimeout(function() {
    console.log("Delay 1");
    count++;
    if (count < 12) {
      if (count % 2 == 0) {
        delay1();
      } else {
        delay2()
      }
    }
    if (callback) {
      callback();
    }
  }, 1000);
}

function delay2(callback) {
  setTimeout(function() {
    console.log("Delay 2");

    if (count > 10) {
      delay1(function() {
        console.log("This is callback;")
      })
    } else if (count % 2 == 0) {
      delay2();
    } else {
      delay1()
    }

    if (callback) {
      callback();
    }
    count++;
  }, 1000);
}


delay1();
Hide result
+1

, . , -. promises .

jquery .

var wait_a_second = $.Deffered();

$.when(wait_a_second)
    .then(does_something_here)
    .then(function(){
        if( cond1 ) delayed1();
    })

    .then(delayed1);


function delayed1(){
    var d = $.Deffered()

    // Your conditions and logic
    if( cond ) {
        d.resolve();
    }

    return d.promise();
}

function delayed2(){
    var d = $.Deffered();

    // Your conditions and logic
    if( cond ) {
        d.resolve();
    }

    return d.promise();
}

promises .

+3

promises JS.

Promise - , , . , . .

Usually you have 3 functions that depend on each other. The first should wait for the second, and the second - to wait for the third. At the first level, the second function should return Promise.

function delayed1() {

    does_something_here();

    setTimeout(function () {
        var promise = do_something1();

        promise.then(function(){
            if ( cond1 ){
               delayed1();
           }
        });

    }, 1000) 
}

Here is your second function. He creates a promise and returns it. Also inside if ( cond3 )he must wait for a promise from the third function. I will not write my third function, as I am sure that you can do it yourself by following this pattern.

function do_something1(){

   return new Promise(function(resolve){
       if ( cond3 ){
           var promise = delayed(2);

           promise.then(function(){ 
              resolve()
            }) 

       }
       else {
          something_else();
          resolve();
       }    
   })  


}
+2
source

Source: https://habr.com/ru/post/1620260/


All Articles