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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:52:22  来源:igfitidea点击:

how to disable a button for 30 seconds

javascriptbutton

提问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

If you use jQuery, this does the job:

如果您使用jQuery,则可以完成以下工作:

$(function() {
    $("button").click(function() {
        $("button").attr("disabled", "disabled");
        setTimeout(function() {
            $("button").removeAttr("disabled");      
        }, 30000);
    });
});

http://jsfiddle.net/vqfKR/

http://jsfiddle.net/vqfKR/

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

您还需要在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>