最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ajaxStart() called only once but ajaxComplete() called multiple times - Stack Overflow

programmeradmin2浏览0评论

I would like to do something on the call of every AJAX request on my page.

I read here that

ajaxStart (Global Event)

This event is broadcast if an Ajax request is started and no other Ajax requests are currently running.

and

ajaxComplete (Global Event)

This event behaves the same as the plete event and will be triggered every time an Ajax request finishes.

This means I can only track the start of one ajax event and not each individual request?

$(document).ajaxStart(function () {
    var t = new Date(),
        h = t.getHours(),
        m = t.getMinutes(),
        s = t.getSeconds(),
        ms = t.getMilliseconds();
    console.log("Triggered ajaxStart handler   at " + h + ":" + m + ":" + s + ":" + ms);
});


$(document).ajaxComplete(function () {
    var t = new Date(),
        h = t.getHours(),
        m = t.getMinutes(),
        s = t.getSeconds(),
        ms = t.getMilliseconds();
    console.log("Triggered ajaxComplete handler at " + h + ":" + m + ":" + s + ":" + ms);
});

gives me

Triggered ajaxStart handler    at 11:14:33:409 
Triggered ajaxComplete handler at 11:14:33:480 
Triggered ajaxComplete handler at 11:14:33:489 
Triggered ajaxComplete handler at 11:14:33:491 
Triggered ajaxComplete handler at 11:14:33:492 
Triggered ajaxComplete handler at 11:14:33:535 
Triggered ajaxComplete handler at 11:14:33:539 
Triggered ajaxComplete handler at 11:14:33:567 
Triggered ajaxComplete handler at 11:14:33:569 

Is there any way to log every ajax start so I can attach an even to every single ajax event?

I would like to do something on the call of every AJAX request on my page.

I read here that

ajaxStart (Global Event)

This event is broadcast if an Ajax request is started and no other Ajax requests are currently running.

and

ajaxComplete (Global Event)

This event behaves the same as the plete event and will be triggered every time an Ajax request finishes.

This means I can only track the start of one ajax event and not each individual request?

$(document).ajaxStart(function () {
    var t = new Date(),
        h = t.getHours(),
        m = t.getMinutes(),
        s = t.getSeconds(),
        ms = t.getMilliseconds();
    console.log("Triggered ajaxStart handler   at " + h + ":" + m + ":" + s + ":" + ms);
});


$(document).ajaxComplete(function () {
    var t = new Date(),
        h = t.getHours(),
        m = t.getMinutes(),
        s = t.getSeconds(),
        ms = t.getMilliseconds();
    console.log("Triggered ajaxComplete handler at " + h + ":" + m + ":" + s + ":" + ms);
});

gives me

Triggered ajaxStart handler    at 11:14:33:409 
Triggered ajaxComplete handler at 11:14:33:480 
Triggered ajaxComplete handler at 11:14:33:489 
Triggered ajaxComplete handler at 11:14:33:491 
Triggered ajaxComplete handler at 11:14:33:492 
Triggered ajaxComplete handler at 11:14:33:535 
Triggered ajaxComplete handler at 11:14:33:539 
Triggered ajaxComplete handler at 11:14:33:567 
Triggered ajaxComplete handler at 11:14:33:569 

Is there any way to log every ajax start so I can attach an even to every single ajax event?

Share Improve this question asked Feb 25, 2013 at 17:22 12527481252748 15.4k34 gold badges117 silver badges242 bronze badges 2
  • 1 I see nothing wrong with what you're written, my only guess is that and no other Ajax requests are currently running. clause is catching you out. – Dunhamzzz Commented Feb 25, 2013 at 17:24
  • @Dunhamzzz My thoughts exactly. Thanks. – 1252748 Commented Feb 25, 2013 at 17:33
Add a ment  | 

1 Answer 1

Reset to default 10

You want the .ajaxSend event, which is sent for every AJAX request, not just the first outstanding one.

发布评论

评论列表(0)

  1. 暂无评论