jQuery:
(function loadAdminMsg() {
$('.activated').each(function () {
loading($(this).data('c_id'),$(this).data('offset'));
});
setTimeout(loadAdminMsg, 10000);
})();
Here loadAdminMsg will be repeated after 10 seconds but if execution of loading($(this).data('c_id'),$(this).data('offset')); will take 11 second then there will be a situation where one process is continuing and another process is in a pipeline that will product unexpected result. How can I confirm loadAdminMsg will be called after 10 second plus execution time of loading($(this).data('c_id'),$(this).data('offset')); ?
loading()method returning promise (e.g ajax request) then use relevant deferred method, e.g,loading($(this).data('c_id'),$(this).data('offset')).always(function(){setTimeout(loadAdminMsg, 10000);})