简体   繁体   English

在Ajax调用完成后运行一个函数

[英]Run a function after an Ajax call has completed

There is something happening that I don't know why is it happening. 我不知道为什么会发生某种事情。 I have this 3 functions: 我有这三个功能:

app.progress();
app.success();
app.normal();

All three change a status block in my application. 这三个都更改了我的应用程序中的状态块。 And I have this $.post in one of my other functions, let's say: 我在其他功能之一中有这个$.post ,可以这样说:

app.set : function() {
  ...
}

In my set function I want to update the status block like this: 在我的set函数中,我想更新状态块,如下所示:

app.set : function() {
    app.progress();
    $.post('ajax.php', function(){
        // do stuffs
    }).done({function(){
        app.success();
        setTimeout(app.normal(), '2000');
    })
}

But somehow, I cannot see the app.success , it skips to app.normal , when I remove the app.normal I can see the app.success . 但是以某种方式,我看不到app.success ,它跳到app.normal ,当我删除app.normal我可以看到app.success

Why is that? 这是为什么?

You're calling app.normal right away, as that is what happens when you add the parenthesis to a function, change this: 您正在立即调用app.normal ,因为在将括号添加到函数时会发生以下情况:

setTimeout(app.normal(), '2000');

to

setTimeout(app.normal, 2000);

referencing the function instead of calling it. 引用函数而不是调用它。

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

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