如何用另一个函数javascript停止计时器

use*_*498 5 html javascript timer timing

所以我有这个代码

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?
}
Run Code Online (Sandbox Code Playgroud)

函数timer()在页面加载时启动但是如果我有一个stopTime()按钮并且我点击它,我如何停止执行第一个函数并阻止它达到3000毫秒标记并提醒"超时" "?

Ama*_*dan 6

使用范围超过所有函数的变量.

var myTimer;
...
myTimer = setTimeout(...);
...
clearTimeout(myTimer);
Run Code Online (Sandbox Code Playgroud)