Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my code which redirects the user to the main page after 10sec of inactivity: 

function endAndStartTimer() {

    window.clearTimeout(timer);

    var timer;

    //var millisecBeforeRedirect = 10000; 

    timer = window.setTimeout(function(){alert('Hello!');},10000); 

}

The code works fine. But the timer doesn’t reset after being inactive. Can anyone tell me how to do cleartimeout? 

1 Answer

0 votes
by (19.7k points)

In your case, since the timer is not declared outside the function it was invoking a new variable on each function. That’s where you faced the issue. 

var timer;

function endAndStartTimer() {

  window.clearTimeout(timer);

  //var millisecBeforeRedirect = 10000; 

  timer = window.setTimeout(function(){alert('Hello!');},10000); 

}

Interested in Java? Check out this Java tutorial by Intellipaat.

Related questions

0 votes
1 answer
asked May 4, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
0 votes
1 answer
asked Feb 26, 2021 in Web Technology by Jake (7k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...