关于A驱动事件和多线程的关系

About the relation ship between A driven event and multithreading

当我在做的时候this try it yourself on W3Schools:

<!DOCTYPE html>
<html>
<body>

<button onclick="startCount()">Start count!</button>
<input type="text" id="txt">
<button onclick="stopCount()">Stop count!</button>

<p>
Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting. Click on the "Start count!" button to start the timer again.
</p>

<script>
var c = 0;
var t;
var timer_is_on = 0;

function timedCount() {
    document.getElementById("txt").value = c;
    c = c + 1;
    t = setTimeout(function(){ timedCount() }, 1000);
}

function startCount() {
    if (!timer_is_on) {
        timer_is_on = 1;
        timedCount();
    }
}

function stopCount() {
    clearTimeout(t);
    timer_is_on = 0;
}
</script>

</body>
</html>

我注意到我内心深处有一个以前从未问过的问题

在本教程中,timedCount() 一直在调用自己,它永远不会停止。 显然。单击停止时,递归停止

请把钥匙指给我。

我认为程序应该永远 运行,这意味着它不会接受任何由按钮驱动的事件

谢谢。

它实际上并不是“运行 永远”,它只是一种看待它的方式。实际上,它会在 1000 毫秒后再次将自己重新安排到 运行。

但是当点击按钮时,stopCount被调用,它放弃了当前的重新安排(clearTimeout(t);)