How to clear old JavaScript running in mobile screen??
Question
Application Type
Mobile

Hi All, 

I need help with the below situation.

I have created a 60sec timer using javascript. It's working fine when I land on the screen for the first time. Now say I click back and go to that screen again, I see that the timer starts behaving wierdly. I found that now there are two instances of timer script running now which is changing the label value. I tried many approaches to clear/kills the instance in OnDestroy event but it doesn't seem to work. Please find my code below

//Timer Logic

var timeleft = $parameters.Timeleft;

var downloadTimer = setInterval(function(){

    document.getElementById($parameters.paramWidgetID).innerHTML = timeleft + " seconds remaining";

    timeleft -= 1;

  if(timeleft < 0){

    clearInterval(downloadTimer);

    document.getElementById($parameters.paramWidgetID).innerHTMLL = "Timed Out";

    $actions.ShowPopup();

  } 

}, 1000);


//Clearing logic on OnDestroy event.

if(window.hasOwnProperty('downloadTimer')) {

window.clearInterval(downloadTimer);

}


Any idea on how to do this ??

Thanks 
Pranav

Hi Pranav,

Are you sure the 'downloadTimer' is found and cleared properly in OnDestroy? Because I think the best way to use clearInterval is to assign the setInterval output to a JS input parameter and then passing it into clearInterval later on, instead of using hasOwnProperty to find it.

$parameters.timeout = setInterval(location.reload(),5000);

and an onDestroy with clearInterval($parameters.timeout);

See this forum post.


If that's not a solution, maybe clicking browser back button doesn't properly fire OnDestroy? Can you confirm that?

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.