[英]javascript setTimeout or jquery delay - neither are working for me
我有這樣的div
<div id="sale">
........
</div>
我試着用這兩個
$('#sale').delay(3000).slideDown(500);
和
setTimeout(sale(), 3000);
function sale() {
$('#sale').slideDown(500);
}
但他們都沒有工作。 jQuery延遲表示$('#sale').delay()
不是函數,而setTimeout
方式表示無用的setTimeout
調用(缺少引號)。 如果我在sale()
調用周圍添加雙引號,它只是說“銷售沒有定義”。
為什么這些都不起作用?
我要做的就是在加載頁面3秒后顯示div。
在setTimeout
情況下,你只是做錯了。
setTimeout(sale(), 3000); // will call sale and use the RETURN value in the callback but sale returns undefined
你需要傳遞一個函數:
function sale() {
$('#sale').slideDown(500);
}
setTimeout(sale, 3000); // just pass in the reference to sale()
其他可能性:
// no difference in this case
// Note: if it were obj.sale() then you would need to do this version
// otherwise sale() will get called with the this set to window
setTimeout(function(){sale()}, 3000)
最后但並非最不重要:
setTimeout(function() { $('#sale').slideDown(500); }, 3000);
你需要在隊列中使delay()
工作。
$('#sale').queue(function() {
$(this).delay(3000).slideDown(500).dequeue();
});
如果您的下一個方法鏈是動畫, Patrick Dw已經在評論中告知您不需要在queue()
。 看他的JSFiddle 。
setTimeout(sale, 3000);
之前,您傳遞了setTimeout
來自sale
的返回值。 這傳遞了實際功能。
在你的第一個解決方案中,似乎jQuery甚至沒有加載。
在第二個代碼中你必須做setTimeout(sale, 3000);
(省略括號),因為有了它們,你使用sale()
的返回來調用setTimeout,這是undefined
。
setTimeout("sale();", 3000);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.