简体   繁体   English

Javascript:在另一个延迟函数内调用延迟函数

[英]Javascript: Calling a delayed function inside another delayed function

So, I have a delayed function called delayed1 that gets repeated everytime cond1 is true, the problem is, somewhere inside that delayed1 function do_something1() gets called and if cond3 is true, another delayed function called delayed2 gets called. 因此,我有一个名为delayed1的延迟函数,每次cond1为真时都会重复,问题是,在delayed1函数do_something1()内部调用,如果cond3为真,则调用另一个名为delayed2的延迟函数。 Now the problem is I want delayed1 to wait until delayed2 is finished (that means until cond2 is false) and then resume again, which doesn't seem to be happening. 现在问题是我想要delayed1等到delayed2完成(这意味着直到cond2为假)然后再次恢复,这似乎没有发生。 Once delayed2 gets started, delayed1 doesn't wait for it to finish and then resume. 一旦delayed2开始,delayed1不会等待它完成然后恢复。 Is there a way to do this? 有没有办法做到这一点? Hopefully 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)

}

I see what your are trying to do here. 我看到你在这里想做什么。 You may probably want to approach in a different way. 您可能希望以不同的方式接近。 The best way would be to use promises or Deferreds . 最好的方法是使用promisesDeferreds

Here is the example using jquery deferreds of your given code. 以下是使用给定代码的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();
}

In short learn promises to manage async operations queue. 总之学习承诺来管理异步操作队列。

You should use promises to implement synchronous execution in JS. 您应该使用promises在JS中实现同步执行。

Promise is a mechanism which returns an object to which you can hook callbacks. Promise是一种返回可以挂钩回调的对象的机制。 These callbacks can be called once a specific function is done with execution. 一旦执行特定功能,就可以调用这些回调。 Here is how it applies to your situation. 以下是它如何适用于您的情况。

You generally have 3 functions which depend on each other. 您通常有3个相互依赖的功能。 The first one should wait for the second and the second one should wait for the third. 第一个应该等待第二个,第二个应该等待第三个。 In the first level the second function should return a Promise . 在第一级,第二个函数应该返回一个Promise

function delayed1() {

    does_something_here();

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

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

    }, 1000) 
}

Here is your second function. 这是你的第二个功能。 It creates a promise and returns it. 它创造了一个承诺并将其返回。 Also inside its if ( cond3 ) it should wait for a promise from the third function. if ( cond3 )它应该等待来自第三个函数的promise。 I'm not going to write your third function as I'm certain that you can do it yourself 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();
       }    
   })  


}

You can try using callbacks. 您可以尝试使用回调。 Following is a basic representation: 以下是一个基本的表示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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