javascript Onclick按钮超时javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9914286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Onclick button timeout javascript
提问by need2nobasis
Can someone help me get started with a button timeout feature. All I want is a button (when clicked) it becomes inactive for 2 seconds. After which it is active again.
有人可以帮助我开始使用按钮超时功能。我想要的只是一个按钮(单击时),它会在 2 秒内变为非活动状态。之后它再次处于活动状态。
回答by Niet the Dark Absol
Start of with:
开始于:
<button>Click me!</button>
Add an event:
添加事件:
<button onClick="...">Click me!</button>
Now we need to put something in place of that ...
.
现在我们需要放一些东西来代替那个...
。
this
can be used to mean "the button that was just clicked"
this
可以用来表示“刚刚点击的按钮”
this.disabled
can be set to true
or false
to disable (or re-enable) the button.
this.disabled
可以设置为true
或false
禁用(或重新启用)按钮。
setTimeout(function() {...},2000);
executes the anonymous function after two seconds have passed (or as near as the timer resolution allows).
setTimeout(function() {...},2000);
在两秒钟过去后(或在计时器分辨率允许的时间内)执行匿名函数。
Again, need to put something in the ...
. I've already told you how to re-enable the button.
再次,需要在...
. 我已经告诉过您如何重新启用按钮。
Although, since this
isn't terribly reliable inside anonymous functions, it's probably better to start with var t = this;
and use t
to mean the button.
虽然,由于this
在匿名函数中并不是非常可靠,所以最好从按钮开始var t = this;
并使用t
它。
With all that in place, you have:
有了所有这些,您就可以:
<button onClick="var t = this; t.disabled = true; setTimeout(function() {t.disabled = false;},2000);">Click me!</button>
Done. I hope this explanation was helpful.
完毕。我希望这个解释有帮助。
PS. To those who are against inline event handlers:
附注。对于那些反对内联事件处理程序的人:
- This is an example
- The OP is a beginner
- An inline event is good enough
- 这是一个例子
- OP是初学者
- 内联事件就足够了
回答by Alex Turpin
The function setTimeout
allows you to specify a function to be called after an amount of milliseconds has passed. In this case, I passed in an anonymous function, that is, a function that does not have a name that is used for the sole purpose of re-enabling my button after 2 seconds.
该函数setTimeout
允许您指定在经过一定数量的毫秒后要调用的函数。在这种情况下,我传入了一个匿名函数,即一个没有名称的函数,其唯一目的是在 2 秒后重新启用我的按钮。
var mybutton = document.getElementById("mybutton");
mybutton.onclick = function() {
mybutton.disabled = true;
setTimeout(function() {
mybutton.disabled = false;
}, 2000);
};?
回答by gdoron is supporting Monica
回答by Web-E
You can use setTimeout() function in javascript. Something like
您可以在 JavaScript 中使用 setTimeout() 函数。就像是
<html>
<head></head>
<body>
<input id="test" type="submit" value = "clickme" onclick="deactivatefunc()">
<script type="text/javascript">
function deactivatefunc()
{
var btn = document.getElementById("test");
btn.disabled = true;
var mytimer = setTimeout(activate,2000);
}
function activate () {
var btn = document.getElementById("test");
btn.disabled = false;
}
</script>
</body>
</html>