最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - how to stop timer with another function javascript - Stack Overflow

programmeradmin0浏览0评论

So I have this code

function timer()
{
     setTimeout(function(){alert("Out of time")}, 3000); //Alerts "Out of time" after 3000 milliseconds
}
function resetTime()
{
     timer(); //this is not right, i thought it would override the first function but it just adds another timer as well which is not what I want
}
function stopTime()
{
     //What could go here to stop the first function from fully executing before it hits 3000 milliseconds and displays the alert message?
}

the function timer() starts as the page loads but if I have a button for stopTime() and I click on it, how do I stop the first function from executing and stop it from hitting the 3000 millisecond mark and alerting "Out of time"?

So I have this code

function timer()
{
     setTimeout(function(){alert("Out of time")}, 3000); //Alerts "Out of time" after 3000 milliseconds
}
function resetTime()
{
     timer(); //this is not right, i thought it would override the first function but it just adds another timer as well which is not what I want
}
function stopTime()
{
     //What could go here to stop the first function from fully executing before it hits 3000 milliseconds and displays the alert message?
}

the function timer() starts as the page loads but if I have a button for stopTime() and I click on it, how do I stop the first function from executing and stop it from hitting the 3000 millisecond mark and alerting "Out of time"?

Share Improve this question asked May 25, 2015 at 5:12 user3450498user3450498 2671 gold badge5 silver badges13 bronze badges 1
  • You need to name your timer to a global var – colecmc Commented May 25, 2015 at 5:13
Add a ment  | 

4 Answers 4

Reset to default 7

Use a variable with scope over all of your functions.

var myTimer;
...
myTimer = setTimeout(...);
...
clearTimeout(myTimer);
var timer;

function timer()
{
    timer = setTimeout(function(){alert("Out of time")}, 3000); //Alerts "Out of time" after 3000 milliseconds
}
function resetTime()
{
    clearTimeout(timer);
     timer(); //this is not right, i thought it would override the first function but it just adds another timer as well which is not what I want
}
function stopTime()
{
     //What could go here to stop the first function from fully executing before it hits 3000 milliseconds and displays the alert message?
}

try this it will Work For you

Its best to use the useRef hook from React

import {useRef} from 'React';


const function =()=>{
   const timerRef = useRef();


   const timerFunction =()=>{
      timerRef.current = setTimeout(()=>{
      //Your Code
      },5000);
`

  const clearTimerFunction =()=>{
       clearTimeout(timerRef.current);
      }

}

The value returned from setTimeout is a unique ID that you can use later to cancel the timeout with clearTimeout.

var timeout;

function timer () {
    timeout = setTimeout(/* ... */);
}

function resetTime() {
    stopTime();
    timer();
}

function stopTime() {
    clearTimeout(timeout);
}
发布评论

评论列表(0)

  1. 暂无评论