javascript 5 秒后 jquery 显示按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31751218/
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
Jquery show button after 5 second
提问by aviva
So I got this button that I need to show after 5 second. I have no experience in jquery or javascript
所以我得到了这个按钮,我需要在 5 秒后显示。我在 jquery 或 javascript 方面没有经验
HTML Code
HTML代码
<body>
<p>You need to wait 0 before you can proceed</p>
<button type="button" id="proceed">proceed</button>
</body>
This is the best that I could come up with after some reading in w3school
这是我在 w3school 阅读后能想到的最好的
<script>
$(document).ready(function(){
$("#show").wait(function(){
delay 0500 $("proceed").show();
});
</script>
Can anyone help me?
谁能帮我?
回答by Barmar
Use setTimeout()to run a function after a time delay:
用于setTimeout()在时间延迟后运行函数:
$(document).ready(function() {
setTimeout(function() {
$("#proceed").show();
}, 5000);
});
#proceed {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>You need to wait 0 before you can proceed</p>
<button type="button" id="proceed">proceed</button>
回答by Ajay Narain Mathur
If you want to display timer setIntervalcan be used.
如果要显示定时器setInterval可以使用。
var i = 4;
var time = $("#time")
var timer = setInterval(function() {
time.html(i);
if (i == 0) {
$("#proceed").show();
clearInterval(timer);
}
i--;
}, 1000)
#proceed {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>You need to wait <span id="time">5</span> before you can proceed</p>
<button type="button" id="proceed">proceed</button>
回答by Eric
If you don't want to use jQuery, then use the setTimeOut method as answered by others.
如果您不想使用 jQuery,请使用其他人回答的 setTimeOut 方法。
<head>
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$('#proceed').delay(5000).show(0);
</script>
</head>
<body>
<p>You need to wait 0 before you can proceed</p>
<button type="button" id="proceed" style="display: none;">proceed</button>
</body>
回答by Jose Mar
The easiest way to postpone a function is:
推迟函数的最简单方法是:
setTimeOut(function(){
//put your code here.
},5000)//5000 millisecond
回答by tredzko
Try this out. .hide()will hide the button after the page loads. .delay(5000)will wait 5000 milliseconds. Then .show(0)will run after that, showing the button.
试试这个。.hide()将在页面加载后隐藏按钮。.delay(5000)将等待 5000 毫秒。然后.show(0)将在此之后运行,显示按钮。
$('#proceed').hide().delay(5000).show(0);
回答by Siddharth Thevaril
Use jQuery's setTimeoutmethod this way.
Below is a working example. A divwill appear 5 seconds after the Run Code Snippetis clicked
以setTimeout这种方式使用 jQuery 的方法。下面是一个工作示例。
单击
Run Code Snippetdiv5 秒后会出现A
$(document).ready(function(){
setTimeout(function(){
$('div').css('display','block');
}, 5000);
});
div {
width: 150px;
height: 150px;
background-color: red;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>
回答by Stanley Li
You have no div section in your code.
您的代码中没有 div 部分。
<div id="showing"></div>
setTimeout(function(){
$('#showing').show();
}, 5000);`
The above example is showing a div section after 5 seconds delays
上面的例子显示了延迟 5 秒后的 div 部分

