javascript 如何禁用按钮 30 秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16242284/
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
how to disable a button for 30 seconds
提问by user2309163
I want to disable a button for 30 seconds after the page is loaded (to prevent multiple clicking without even a try to watch the content). How can I do that?
我想在页面加载后禁用按钮 30 秒(以防止多次点击甚至没有尝试观看内容)。我怎样才能做到这一点?
<input class="button vote" type="submit" onClick="javascript: vote();" value="Vote for this story" />
回答by jgillich
回答by Bucket
Add the attribute disabled="disabled"
to your button.
将属性添加disabled="disabled"
到您的按钮。
Set a timeout:
设置超时:
window.onload = function() {
window.setTimeout(setDisabled, 30000);
}
Then define what to do after that timeout:
然后定义超时后要做什么:
function setDisabled() {
document.getElementById('yourButton').disabled = false;
}
You'll also need to add an id of 'yourButton' to your input
tag.
您还需要在input
标签中添加一个 id 'yourButton' 。
回答by DiMono
You're looking for setTimeout(). Give your input id='votebutton' disabled="disabled"
and then include this at the bottom of your page:
您正在寻找 setTimeout()。提供您的意见id='votebutton' disabled="disabled"
,然后将其包含在页面底部:
<script type='text/javascript'><!--
setTimeout(function() {
document.getElementById('votebutton').disabled = false;
}, 30000);
</script>