Logo BrocksiNet

BrocksiNet

Try to clean the web

JavaScript check if pending ajax calls exist

Sometimes you have to wait until some ajax calls are finished. And then you want to execute some JavaScript code on that new objects that where putted in from the ajax call. To check if there are activeRequests you can use window.Ajax.activeRequestCount. And to re-check if they are now finished you can use window.setInterval.

Important is also to clear the intervalID with window.clearInterval(intervalID).

Here is a small code snippet

// we need to wait for ajax calls to be finished
var intervalID = window.setInterval(
    function() {
        if(window.Ajax.activeRequestCount === 0) {
            console.log('No active ajax calls');
            window.clearInterval(intervalID);
        } else {
            console.log('There are ' + window.Ajax.activeRequestCount + ' active ajax requests');
        }
}, 500);
Released - 01.08.2017

Comments


About the author

Bild Bjoern
Björn MeyerSoftware Engineer
Björn is interested in the details of web technologies. He started with Joomla, Drupal, Typo3, vBulletin, Fireworks and Photoshop. Today his focus is on PHP for example with Magento 2, Shopware 6 and Symfony. He also likes JavaScript with Angular, React, VueJs and TypeScript. In his free time, he seeks constant balance with swimming, running and yoga.