2

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')); ?

1
  • Makes 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);}) Commented Oct 8, 2015 at 13:13

1 Answer 1

1

You should add a callback function parameter to your loading() function and call it when your loading finishes.

function loading(c_id, offset, fnCallback) {
   // do stuff stuff stuff
   // ...
   fnCallback();
}

(function loadAdminMsg() {
  $('.activated').each(function () {
     loading($(this).data('c_id'),$(this).data('offset'), function() {
        setTimeout(loadAdminMsg, 10000);
     });
  });

})();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.