user2881803
user2881803

Reputation: 3

how to stop Javascript function execution?

My program has two buttons. One is for execute other program by using jquery load function. Whenever I click execute button, it runs some other program abc.php using load function for n times, with some time gap like k mins. These n and k will be filled with html inputs. Using jquery, will retrieve these and passing to that program file in url.

To call this function setTimeout was used.

Second one is for cancel execution.

Now my doubt is, suppose I want to stop that execution with cancel button. Is there any way to stop it ?

Upvotes: 0

Views: 6133

Answers (2)

Mr.G
Mr.G

Reputation: 3559

set time for function:

timer = setTimeout(function(){$('#submenu').hide();},5000);

stop a function

clearTimeout(timer);

Upvotes: 0

Praveen
Praveen

Reputation: 56509

I would do this using boolean variable.

For example: Consider a method, perform logging.

fun () {
  console.log("prints");
}

I would change it has

fun (isExecute) {
  if (isExecute) {
   console.log("prints");
  }
}

Run fun (true); cancel fun (false);


Updates:

It seems you use setTimeout(), then it is too easy without above approach.

Run var inter = setTimeout(fun); cancel clearTimeout(inter);

FYI: The reason for assigning to a variable inter is then only you can clear this time interval.

Upvotes: 4

Related Questions