doniyor
doniyor

Reputation: 37934

how to kill js function in another function's scope

i built the please wait gif animator for my form submission, because the registration takes a bit more time.

now everything works fine, but my gif animation still keeps running even after success message. Thus my animation function is still running. how can i kill that function?

this is what i did now:

function loadSubmit(){
  ProgressImage = document.getElementById('progress_image');
  document.getElementById("progress").style.visibility = "visible";
  setTimeout("ProgressImage.src = ProgressImage.src",100);
  return true;
}

and i call this before my ajax function:

 loadSubmit();
 $.ajax({
   url: "/register_me/",
   type: "POST",
   ...
 }).done(function(){
      // can i somehow kill loadSubmit() function in this scope? 
 });

this would be so good to be able to do it..

thanks a lot

Upvotes: 1

Views: 93

Answers (1)

vdua
vdua

Reputation: 1331

setTimeout returns a integer value which can then be used to clear that timeout using the function clearTimeout

function loadSubmit(){
   ProgressImage = document.getElementById('progress_image');
   document.getElementById("progress").style.visibility = "visible";
   // what exactly you want you achieve through this timeout
   return setTimeout("ProgressImage.src = ProgressImage.src",100);

}

var intervalID = loadSubmit();
$.ajax({
  url: "/register_me/",
  type: "POST",
  ...
}).done(function(){
   document.getElementById("progress").style.display = "none"; //EDIT
   clearTimeout(intervalID)
});

Read the documentation for setTimout at MDN https://developer.mozilla.org/en-US/docs/DOM/window.setTimeout

EDIT: I misread the question. you need to hide the ProgressImage to hide the gif, you still need to clear the timeout.

Upvotes: 2

Related Questions