秒後にページの背景色を変更するためのタイマーを作成しましたが、ユーザーが間隔を変更できるようにしようとしました... 私は、ユーザーがボタンをクリックすると、間隔がテキストボックスに番号を変更しますが、それは働いていなかったので...これは私のコードで番号入力し、ボタンを追加しました:変更するテキストボックスを作成する方法JavaScriptとhtmlを使用してsetIntervalの間隔を
var pos = 0;
var interval = 1000; //interval of the timer
function cbc() //function to change the background color
{
var bgclr = document.getElementById("bd")
switch (pos) {
case 0:
pos = 1;
bgclr.style.backgroundColor = "red";
break;
case 1:
pos = 0;
bgclr.style.backgroundColor = "blue";
break;
}
}
function execute() //function to be executed when the execute button is clicked
{
var input = document.getElementById("textbox1");
interval = input.value;
}
myTimer = setInterval(cbc, interval);
<input type="number" style="width:40px" id="textbox1">
<button onclick="execute">Execute</button>
ありがとうございます。
https://stackoverflow.com/questions/1280263/changing-the-interval-of-setinterval-while-its-running –
[実行中のSetIntervalの間隔を変更する]の可能な複製(https:// stackoverflow .com/questions/1280263/setting-interval-of-the-running実行中) –