JavaScriptで関数の実行を停止するにはどうすればよいですか?
JavaScriptで関数の実行を停止するには、clearTimeout()メソッドを使用します。この関数呼び出しは、setTimeout()関数によって設定されたタイマーをすべてクリアします。
例
次のコードを実行して、JavaScriptでclearTimeout()メソッドを操作する方法を学ぶことができます。
<html>
<head>
<title>JavaScript clearTimeout() method</title>
<script>
<!--
var imgObj = null;
var animate ;
function init(){
imgObj = document.getElementById('myImage');
imgObj.style.position= 'relative';
imgObj.style.left = '0px';
}
function moveRight(){
imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px';
animate = setTimeout(moveRight,20); // call moveRight in 20msec
}
function stop(){
clearTimeout(animate);
imgObj.style.left = '0px';
}
window.onload = init;
//-->
</script>
</head>
<body>
<form>
<img id = "myImage" src = "/images/html.gif" />
<p>Click the buttons below to handle animation</p>
<input type = "button" value = "Start" onclick = "moveRight();" />
<input type = "button" value = "Stop" onclick = "stop();" />
</form>
</body>
</html> -
JavaScriptを使用してテキストをクリップボードにコピーするにはどうすればよいですか?
以下は、JavaScriptを使用してテキストをクリップボードにコピーするためのコードです- 例 <!DOCTYPE html> <html> <head> <style> button { border: none; outline: none; background-color: rgb(191, 187, 255); color: black; font-weight: bold; pa
-
JavaScriptでの実行中に関数を停止するにはどうすればよいですか?
実行中に関数を停止するには、-の概念を使用します。 document.getElementById().addEventListener(). 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document&