简体   繁体   中英

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:

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

In my set function I want to update the status block like this:

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 .

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:

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

to

setTimeout(app.normal, 2000);

referencing the function instead of calling it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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