gpt4 book ai didi

JavaScript - 在焦点事件上停止计时器

转载 作者:行者123 更新时间:2023-11-30 12:43:06 25 4
gpt4 key购买 nike

我有一个脚本,每 2 秒更改一次文本框中的文本。我想让它停止(计时器)并在用户单击并关注文本框时清除文本框。

var text = ["1", " 2", "3"];
var counter = 0;
var elem = document.getElementById("txt1");

setInterval(change, 2000);

function change() {
document.getElementById("txt1").value = text[counter]; //data is the element
counter++;
if(counter >= text.length) {
counter = 0;
}
}

最佳答案

像这样更新你的输入类型

 <input type="text" onfocus="myFunction()">

同时更新

setInterval(change, 2000);

var tt=setInterval(change, 2000);

在脚本中添加 myFunction

function myFunction(){
clearInterval(tt);
document.getElementById("txt1").value = "";
}

关于JavaScript - 在焦点事件上停止计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23656108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com