Javascript 设置超时完成后运行下一个函数

Javascript 设置超时完成后运行下一个函数,javascript,jquery,Javascript,Jquery,如何让它工作?请帮忙 function first() { setTimeout((function() { $('#q').append('first <br>'); }), 1000); } function second() { $('#q').append('second <br>'); } function third() { $('#q').append('third <br>'); } $.when

如何让它工作?请帮忙

function first() {
    setTimeout((function() {
        $('#q').append('first <br>');
    }), 1000);
}
function second() {
    $('#q').append('second <br>');
}
function third() {
    $('#q').append('third <br>');
}
$.when(first()).done(second()).done(third()); 
函数优先(){
setTimeout((函数(){
$('q')。追加('first
'); }), 1000); } 函数第二(){ $('#q')。追加('second
'); } 函数第三(){ $('#q')。追加('third
'); } $.when(first()).done(second()).done(third());
first()作为最后一个函数运行,我需要作为第一个


在这里摆弄:

我不知道您为什么要这样做,但是如果您想同步执行它们,可以将第二个和第三个函数调用放在
setTimeout
中:

function first() {
    setTimeout(function() {
        $('#q').append('first <br>');
        second();
        third();
    }, 1000);
}
function second() {
    $('#q').append('second <br>');
}
function third() {
    $('#q').append('third <br>');
}
first();
函数优先(){
setTimeout(函数(){
$('q')。追加('first
'); 第二个(); 第三(); }, 1000); } 函数第二(){ $('#q')。追加('second
'); } 函数第三(){ $('#q')。追加('third
'); } 第一个();
如果要隔离函数,请尝试此操作。您将
回调
传递给
first
函数,当计时器超时时,调用该回调。在该回调中,您可以调用
second()
third()

函数优先(回调){
setTimeout(函数(){
$('q')。追加('first
'); 回调(); }, 1000); } 函数第二(){ $('#q')。追加('second
'); } 函数第三(){ $('#q')。追加('third
'); } 第一(函数(){ 第二个(); 第三(); });
有几个问题:您的
第一个
函数(以及其他函数)没有返回承诺,因此您不能使用
done
。如果您正在使用承诺,
done
将封存承诺,并且不允许您链接另一个
done
呼叫。对于此设置,最好嵌套函数调用,如:

function first() {
    setTimeout((function() {
        $('#q').append('first <br>');
        second();
    }), 1000);
}
function second() {
    $('#q').append('second <br>');
    third();
}
function third() {
    $('#q').append('third <br>');
}
函数优先(){
setTimeout((函数(){
$('q')。追加('first
'); 第二个(); }), 1000); } 函数第二(){ $('#q')。追加('second
'); 第三(); } 函数第三(){ $('#q')。追加('third
'); }
试试这个

function first() {
    setTimeout((function() {
        $('#q').append('first <br>');
        second();
        third();
    }), 1000);
}
function second() {
    $('#q').append('second <br>');
}
function third() {
    $('#q').append('third <br>');
}
first();
函数优先(){
setTimeout((函数(){
$('q')。追加('first
'); 第二个(); 第三(); }), 1000); } 函数第二(){ $('#q')。追加('second
'); } 函数第三(){ $('#q')。追加('third
'); } 第一个();
编辑: 您现在还可以尝试(不支持IE)在执行第一秒和第三秒之前等待超时承诺
fireEvents
是本例中的异步函数。请注意,因为
firevents
在函数前面有
async
关键字,所以它可以使用
wait
键盘。等待关键字允许您等待承诺的完成。这里有一个使用setTimeout构造函数的小型promise包装器,它是从函数
timeout
返回的。这个
timeout
函数在异步函数
fireEvents
中等待。下面的注释数字显示了执行顺序。关于承诺执行令的更深层次的知识,你可以阅读杰克·阿奇博尔德的精彩文章

功能超时(毫秒){
返回新承诺(res=>setTimeout(res,ms));
}
函数优先(){
//$('q')。追加('first
'); 控制台日志(“第一”); } 函数第二(){ //$('#q')。追加('second
'); 控制台日志(“第二”); } 函数第三(){ //$('#q')。追加('third
'); 控制台日志(“第三”); } 异步函数fireEvents(){ // 2 console.log(“2.等待前”) 等待超时(1000); // 4 控制台日志(“4.等待后”) 第一个(); 第二个(); 第三(); } // 1 console.log(“1.started”); fireEvents()。然后(()=>{ // 5 console.log(“5.done”) }); // 3
控制台日志(“3.火灾事件后”)
尝试在
第一个
中使用
$.Deferred()
,返回
$.Deferred()
jQuery promise当
设置超时
完成时,调用
第二个
第三个
.done()

函数优先(){
var d=新的$.Deferred();
setTimeout((函数(){
$('q')。追加('first
'); d、 解决() }), 1000); 返回d.承诺() } 函数第二(){ 返回$('#q')。追加('second
'); } 函数第三(){ 返回$('q')。追加('third
'); } $.when(first()).done([second,third])

试试:

函数开始(){
setTimeout((函数(){
$.when(first()).done(second()).done(third());
}), 1000);
}
函数优先(){
$('q')。追加('first
'); } 函数第二(){ $('#q')。追加('second
'); } 函数第三(){ $('#q')。追加('third
'); } start();
您是否尝试在超时函数本身的末尾编写代码?这些setTimeout是异步的,因此我们不知道将首先执行哪一个
function first() {
    setTimeout((function() {
        $('#q').append('first <br>');
        second();
        third();
    }), 1000);
}
function second() {
    $('#q').append('second <br>');
}
function third() {
    $('#q').append('third <br>');
}
first();
function start() {
    setTimeout((function () {
        $.when(first()).done(second()).done(third());
    }), 1000);
}
function first() {
    $('#q').append('first <br>');
}
function second() {
    $('#q').append('second <br>');
}
function third() {
    $('#q').append('third <br>');
}

start();