Question

get_user_record() this function call the method which is pulling data in database. I used time-out because I don’t want response from this method, showUpdatedProgressBar() method which is continuously checking database count and accordingly giving value to progress bar. For that purpose I used setInterval() function which is working but I was not able to clear the interval. Please suggest me where I go wrong.

function get_user_record(){
        $.ajax({
                url:"/GetData",
                type: "GET",
                timeout: 2000,
                success:function(result){
                    //alert('success');
                },  
                error: function(xhr, status, err){ 
                    //alert('Connection Error. Please try again.')
                }

            });
            var timer = 0;
            showUpdatedProgressBar(timer);
        }

    }
    function showUpdatedProgressBar(timer){

        $.ajax({
            url:"/get_updated_data",
            type: "GET",
            success:function(result){
                result = result.split(',');
                var obj = {totalRecords: result[0], recordsTaken: result[1]};
                var bar_value = obj.recordsTaken/obj.totalRecords * 100;
                $( "#progressbar" ).progressbar({ value: bar_value });

                if(obj.recordsTaken == obj.totalRecords ){
                    clearInterval(timer);                   
                }
                else
                {
                    timer = setInterval(function(){ showUpdatedProgressBar(timer) },1000);
                }
            }           
        });
    }
Was it helpful?

Solution

Previously I defined var timer locally and set to 0 Now it works just by defining var timer; globally and not setting it to zero

OTHER TIPS

timer = setInterval(function(){ showUpdatedProgressBar(timer) },1000);

'timer' is assigned a new interval id with every recursive iteration (it is overwritten). Therefore only the last generated id will be used in clearInterval.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top