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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:12:40  来源:igfitidea点击:

Onclick button timeout javascript

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 ....

现在我们需要放一些东西来代替那个...

thiscan be used to mean "the button that was just clicked"

this可以用来表示“刚刚点击的按钮”

this.disabledcan be set to trueor falseto disable (or re-enable) the button.

this.disabled可以设置为truefalse禁用(或重新启用)按钮。

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 thisisn't terribly reliable inside anonymous functions, it's probably better to start with var t = this;and use tto 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:

附注。对于那些反对内联事件处理程序的人:

  1. This is an example
  2. The OP is a beginner
  3. An inline event is good enough
  1. 这是一个例子
  2. OP是初学者
  3. 内联事件就足够了

回答by Alex Turpin

The function setTimeoutallows 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);
};?

Live example

活生生的例子

回答by gdoron is supporting Monica

<input type="button" value="click" id="click" onclick="foo(this);"/>?

function foo(obj) {
    obj.disabled = true;
    setTimeout(function() {
        obj.disabled = false;
    }, 2000);
}?

LIVE DEMO

现场演示

window.setTimeouton MDN:

window.setTimeoutMDN 上

Executes a code snippet or a function after specified delay.

在指定延迟后执行代码片段或函数。

回答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>