Skip to main content

[jquery] Update widget every 10 secs by Ajax

var updateWidgets = (function() {
            var UPDATE_WIDGETS_INTERVAL = 10000,
                containers = {
                    nps: $('#nps-widget-container'),
                    feedback: $('#feedback-widget-container')
                };

            return function() {
                setTimeout(function() {
                    $.ajax({
                        url: "URL",
                        type: "GET",
                        dataType: "json"
                    }).done(function(data) {
                        $.each(containers, function(name, $container) {
                            if (data.hasOwnProperty(name)) {
                                $container.html(data[name]);
                            }
                        });
                    }).always(updateWidgets);
                }, UPDATE_WIDGETS_INTERVAL);
            };
        })();

Comments