3

I'm wondering if there is any way to check if there are any pending calls that'll be executed at some point, like for example a callback from an AJAX request or a timer (setTimeout).

Something like checking the entire stack of the engine that is running JavaScript.

alexandernst
  • 14,352
  • 22
  • 97
  • 197
  • 2
    This partly answers the question : [there is no accessible enumeration of pending timers](http://stackoverflow.com/questions/2814919/does-the-browser-keep-track-of-active-timer-ids). – Denys Séguret Nov 28 '12 at 11:49
  • @dystroy Yes indeed, that answers partially the question :) But there are a lot of methods to "queue" a call, checking each of them "manually" is nearly impossible. There must be some other way. – alexandernst Nov 28 '12 at 11:57
  • I never saw any API for this in the spec (HTML or ecmascript) but it's hard to prove there is no API... – Denys Séguret Nov 28 '12 at 11:58
  • BTW, not to say your question isn't interesting but, apart doing some debug, what would be the practical use of such an enumeration ? – Denys Séguret Nov 28 '12 at 12:01
  • 1
    @dystroy Headless browsers need to know *when* exactly the page finished "loading". They cant trust the "ready state" of the page, as there may be some dynamic content, or even a MVC library that will change completely the downloaded content. The idea (at least for the headless browser I'm working on) is to fetch a page and return the HTML that the end user will see (instead of the raw content fetched from the server). – alexandernst Nov 28 '12 at 12:06
  • 1
    You can store a reference to a `setTimeout` and a `setInterval` and refer to it later (to clear it), but this is the closest I can think of. It would be really useful if it could be done though. – starbeamrainbowlabs Nov 28 '12 at 12:10
  • I'm not so sure. There are many cases (automatic periodic fetching, websocket alimentation, etc.) where there is no relation with a "loaded" state and the requests or exchanges. If you have a specific notion of when your page is loaded, you should keep some trace of what needs loading. – Denys Séguret Nov 28 '12 at 12:12
  • [This question](http://stackoverflow.com/questions/9365065/in-javascript-get-the-callstack-that-lead-to-error) discusses a similar topic. – undefined Nov 28 '12 at 13:48
  • [This too](http://stackoverflow.com/questions/7049510/how-to-view-all-javascript-functions-called-in-real-time) – undefined Nov 28 '12 at 13:54

1 Answers1

2

Considering that AJAX callbacks are dependent on the server response(success, failure), you can't define if they are pending to be called, until it's actually time to call them.

But here's an idea how this checking can be achieved for setTimeout (and maybe setInterval):

window.timeoutsRegistry = [];

window.oldSetTimeout = window.setTimeout;

window.setTimeout = function(func, delay) {
    var tId = window.oldSetTimeout(function() {
        try {
            func();
        }
        catch (exception) {
            //Do Error Handling
        }
    }, delay);

    var startedAt = (+new Date);

    window.timeoutsRegistry[tId] = {
        id: tId,
        callback: func,
        delay: delay,
        startedAt: startedAt,
        isPending: function () {
            var now = (+new Date);

            return ((startedAt + delay) > now);
        }
    };
};

for(var i =0; i < 10; i++) {
    setTimeout(function() {
        1+1;
    }, 200000);
}


console.log(window.timeoutsRegistry);

var pending = window.timeoutsRegistry.filter(function(element) {
    return element.isPending();
});

console.log(pending);

Some notes:

Community
  • 1
  • 1
undefined
  • 2,051
  • 3
  • 26
  • 47