0

I am calling an ajax polling function when a particular bootstrap modal gets open, it works well, but when I close the BS Modal forcefully the polling function should stop, but it keeps on firing in the background.

I don't know what I am doing wrong. Using Bootstrap 4.

Here is my js script,

var run = function() {

    $.ajax({
        url: ajaxurl,
        dataType: 'json',
        type: 'GET',
        data: data,
        success: function (data) {
            var list = data.list;

            $("#modal").on('hidden.bs.modal', function (e) {
                clearTimeout(recursive);
                console.log('stop');
            });

            if (list > 0) {
                recursive = setTimeout(run, 2000);  
                console.log('run');
            } else if (list == 0) {
                clearTimeout(recursive);
                console.log('stop');
            }

        }
    });
}

Update:

I tried it this way, but not working.

var recursive;

$("#modal").on('shown.bs.modal', function (e) {
    run();
});

$("#modal").on('hidden.bs.modal', function (e) {
    clearTimeout(recursive);
});

var run = function() {

    $.ajax({
        url: ajaxurl,
        dataType: 'json',
        type: 'GET',
        data: data,
        success: function (data) {
            var list = data.list;

            if (list > 0) {
                recursive = setTimeout(run, 2000);  
                console.log('run');
            } else if (list == 0) {
                clearTimeout(recursive);
                console.log('stop');
            }

        }
    });
}
Deepak Singh
  • 129
  • 2
  • 12
  • Have you checked to make sure that your ajax call has actually succeeded? – Difster Dec 27 '18 at 19:16
  • @difster Yes, ajax working perfectly, and setTimeout() function doing its job, but when I close the BS Modal clearTimeout() function should work but it is not working. and it keeps on polling. – Deepak Singh Dec 27 '18 at 19:20
  • @Taplar I tried declaring var recursive; outside my run function, but no success. Yes, but I am not sure about bs modal part, where should I call that? – Deepak Singh Dec 27 '18 at 19:30
  • Yes, modal exits before function run execute. Actually, I am binding function run with 'shown.bs.modal' event and trying to stop polling it with 'hidden.bs.modal' event. Now I am calling clearTimeout() outside the run function but no success. – Deepak Singh Dec 27 '18 at 19:43
  • @Taplar Please check the updates in the description, are you saying to do something similar? – Deepak Singh Dec 27 '18 at 19:52
  • Yeah, that looks good. Now my next curiosity is that, this logic could have a logical flaw in it. If you close the modal while the ajax is running, then it would clear the timeout in your hidden handler, but the ajax would create a new one when it goes through the success method. Clearing the timeout isn't going to terminate any ajax requests currently executing. – Taplar Dec 27 '18 at 19:53
  • Okay, then how could I terminate it? – Deepak Singh Dec 27 '18 at 19:58

0 Answers0