How can I stop this setTimeout function from running?

javascript, jquery

Solution

Before your call of `checkBookStatus()` at the end, put another call: `var interval = setInterval(checkBookStatus, 8000);`. Then on success you can `clearInterval(interval)`.

Do not use `setTimeout` for iteration.

A lot of answers are suggesting just to use `clearTimeout()` however, you are checking the status after the timeout has expired, there is no timeout to clear. You need to not call `setTimeout()` in your `always()` function rather than to clear anything. So you could re-inspect the status inside your `always()` function I suppose, but your `data` object isn't in scope there. It would be preferable to just use `setInterval()` outside of your `checkBookStatus()` function.

$(function() {
    var checkBookStatus = function() {
        var job_id = "#{@job.job_id}";
        var msg = $('.msg');
        var msgBuilding = $('#msg-building');
        var msgQueuing = $('#msg-in-queue');
        var msgSuccessful = $('#msg-successful-build');
        var msgError = $('#msg-error'); 
        $.ajax({ 
            url: '/jobs/'+job_id+'/status.json',
            datatype: 'JSON',
            success:function(data){
                if (data.status == "failure")  {
                    msg.hide();          
                    msgError.show();
                }
                else if (data.status == "#{Job.queue}")  {
                    msg.hide();          
                    msgQueuing.show();
                }
                else if (data.status == "#{Job.building}")  {
                    msg.hide();          
                    msgBuilding.show();
                }             
                else if (data.status == "#{Job.failure}")  {
                    msg.hide();          
                    msgError.show();
                }
                else if (data.status == "#{Job.success}")  {
                    msg.hide();          
                    msgSuccessful.show();
                    clearInterval(interval);
                }                    
            }       
        });
    };    
    var interval = setInterval(checkBookStatus, 8000);    
    checkBookStatus();
});

Problem

I use the function below to check on the status of a JSON file. It runs every 8 seconds (using setTimeout) to check if the file has changed. Once the JSON's status becomes 'success' I no longer want to keep calling the function. Can someone please show me how to do this? I suspect it involves the use of clearTimeout, but I'm unsure how to implement this. Cheers! ``` $(function() { var checkBookStatus = function() { var job_id = "#{@job.job_id}"; var msg = $('.msg'); var msgBuilding = $('#msg-building'); var msgQueuing = $('#msg-in-queue'); var msgSuccessful = $('#msg-successful-build'); var msgError = $('#msg-error'); $.ajax({ url: '/jobs/'+job_id+'/status.json', datatype: 'JSON', success:function(data){ if (data.status == "failure") { msg.hide(); msgError.show(); } else if (data.status == "#{Job.queue}") { msg.hide(); msgQueuing.show(); } else if (data.status == "#{Job.building}") { msg.hide(); msgBuilding.show(); } else if (data.status == "#{Job.failure}") { msg.hide(); msgError.show(); } else if (data.status == "#{Job.success}") { msg.hide(); msgSuccessful.show(); } }, }).always(function () { setTimeout(checkBookStatus, 8000); }); }; checkBookStatus(); }); ```

Original source