Question

I have polling question for you. I am polling information every 20 seconds using setInterval, and have click events that when fired, they pass objects to a method. The problem comes when I click on a button and the event fires at the same time the poll restarts. If that happens, the object that is passed is undefined, which makes sense because when we restart the poll, the information is refreshed. So the question is, how/can one "block" an event from firing when a setInterval is restarting?

Thanks

Was it helpful?

Solution

Going to show some dummy code to illustrate the idea of a queue while you refresh the data on the page:

$(function() {
   var isRefreshing = true;
   var queue = [];
   setInterval(function() {
        isRefreshing = true;
        $.ajax({
            /*
            settings
            */
            success: function() {
                isRefreshing = false;

                //process queue
                var item;
                while(item = queue.pop()) {//if order matters use shift
                    worker(item);
                }
            }
        });
   }, 20*1000);

   var worker = function(/*params*/) {
        //stuff
   }

   $("#my-element").click(function() {
        var data = {};
        if(isRefreshing) {
            queue.push(data)
        } else {
            worker(data);
        }
   }); 
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top