【问题标题】:need a delay between 2 function each has set time out需要两个函数之间的延迟,每个函数都设置了超时
【发布时间】:2013-12-05 13:59:31
【问题描述】:

我有 2 个函数 abc() 和 def()。两者都具有 settimeout 功能。它们是从父 xyz() 函数调用的。最初应调用 abc(),然后应调用 abc 动画 def() 功能。问题:这两个函数一个接一个地被调用,但它们之间没有延迟。两个 settimeout 都同时工作。请帮忙。提前致谢。

function abc() {
    // there is settimeout function
    //cleartimeout after some time 
}

function def() {
    // there is settimeout function
    //cleartimeout after some time 
}

function xyz() {
    abc(); // i need a delay between each settimeout functionality. 
    def(); //currently both run together.
}

【问题讨论】:

    标签: jquery animation delay settimeout intervals


    【解决方案1】:

    一种解决方案是在xyz() 中进行超时

    function abc() {
        // Leave this alone, no need for setTimeout in here
    }
    
    function def() {
        // Leave this alone, no need for setTimeout in here
    }
    
    function xyz() {
        abc(); // i need a delay between each setTimeout functionality. 
        setTimeout(def, 500);
    }
    

    如果def() ALWAYS 出现在abc() 之后,那么您可以让xyz() 简单地调用abc() 并在abc() 内,您可以在动画结束后调用def(),但我们会需要看abc()的代码=)

    【讨论】: