源代碼
點擊運行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>教程</title> </head> <body> <p>點擊第一個按鈕等待3秒後出現"Hello"彈框。</p> <p>點擊第二個按鈕來阻止第一個函數運行。(你必須在3秒之前點擊它)。</p> <button onclick="myFunction()">點我</button> <button onclick="myStopFunction()">停止彈框</button> <script> var myVar; function myFunction(){ myVar=setTimeout(function(){alert("Hello")},3000); } function myStopFunction(){ clearTimeout(myVar); } </script> </body> </html>
運行結果