[英]loading a div 1 minute after the page loads
我在主頁上有一個帶有id通知的div。 我不希望在第一次加載頁面時加載div,我希望它在1分鍾后加載。 在javascript或jquery中有什么方法可以做我想要的嗎?
我不確定你想如何將你的div實際添加到DOM中,但你可以使用setTimeout來延遲一分鍾。 來自文檔 :
Calls a function or executes a code snippet after specified delay.
所以像這樣:
function appendDiv() {
... append code here...
}
timeoutID = window.setTimeout(appendDiv, 60000);
當然:)在我的例子中我使用jQuery:
<div style="display:none" id="notifications"></div>
<script type="text/javascript">
$(document).ready(function() {
setTimeout(function() {
$.get('/notifications.php', function(result) {
// build your notifications here
// ...
$('#notifications').show();
}, 'json');
}, 60000);
});
</script>
如果第一分鍾通知不應該在DOM中,請使用以下代碼:
$(document).ready(function(){
setTimeout( function(){
$("<div>")
.html( "your notification message/markup" )
.attr({ id : 'notification', ... })
.appendTo( "body" /* or any other DOM element */ );
}, 60*1000);
})
使用jQuery的load
函數:
setTimeout(function() {
$('#myDiv').load('divcontents.html');
}, 60 * 1000);
divcontents.html
可以在服務器上動態生成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.