• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Stoping a javascript function.

new2AMD

Diamond Member
I have a start button calling a function. I want a stop button to stop that same function. Is there something like unload or onclick that will stop the function that is running?
 
Without seeing what you are trying to do, I'll throw a thought out there. Declare a global variable (blRun) set to true.

For the Stop button, have an onClick action that sets blRun = false;

For the Start function, simply add checks for blRun. If it's false, then return. If true, keep going.

Trevor
 
Originally posted by: hellman69
Without seeing what you are trying to do, I'll throw a thought out there. Declare a global variable (blRun) set to true.

For the Stop button, have an onClick action that sets blRun = false;

For the Start function, simply add checks for blRun. If it's false, then return. If true, keep going.

Trevor

This will work, but make sure you use setTimeout() to loop your function, or you'll likely lock up the browser. Something like:

var bRun = false;

function myFunction(){
....if (bRun){
........// do some stuff
........setTimeout('myFunction()',1000);
....}
}

function startFunction(){
....bRun = true;
....setTimeout('myFunction()',1000);
}

function stopFunction(){
....bRun = false;
}
 
Back
Top