javascript setTimeout 或 jquery 延迟 - 两者都不适合我
我有一个像这样的 div
<div id="sale">
........
</div>
,我尝试使用两者
$('#sale').delay(3000).slideDown(500);
,
setTimeout(sale(), 3000);
function sale() {
$('#sale').slideDown(500);
}
但它们都不起作用。 jQuery 延迟表示 $('#sale').delay()
不是函数,而 setTimeout
方式表示无用的 setTimeout
调用(缺少引号)。如果我在 sale()
调用周围添加双引号,它只会显示“Sale is not Defined”。
为什么这些都不起作用?
我想做的就是让 div 在页面加载后 3 秒出现。
I have a div
like this
<div id="sale">
........
</div>
and I tried to use both
$('#sale').delay(3000).slideDown(500);
and
setTimeout(sale(), 3000);
function sale() {
$('#sale').slideDown(500);
}
but neither of them are working. The jQuery delay says $('#sale').delay()
is not a function while the setTimeout
way says useless setTimeout
call (missing quotes). If I add double quotes around the sale()
call, it just says "Sale is not defined".
Why won't either of these work?
All I'm trying to do is make a div appear 3 seconds after the page is loaded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在你的第一个解决方案中,似乎 jQuery 甚至没有加载。
在第二个代码中,您必须执行
setTimeout(sale, 3000);
(省略括号),因为使用它们,您将使用sale( 的 return 调用 setTimeout )
,这是未定义
。In your first solution it seems that jQuery is not even loaded.
In the second code you have to do
setTimeout(sale, 3000);
(omit the parentheses) because with them you are calling setTimeout with the return ofsale()
, which isundefined
.如果是
setTimeout
你只是做错了。您需要传入一个函数:
其他可能性:
最后但并非最不重要的:
In case of
setTimeout
you're simply doing it wrong.You need to pass in a function:
Other possibility:
And last but not least:
您需要排队才能让
delay()
工作。查看它。
Patrick Dw 在评论中告知,如果您的下一个方法链是动画,则不需要位于
queue()
中。请参阅他的 JSFiddle。You need to be in a queue for
delay()
to work.See it.
Patrick Dw has informed in the comments you don't need to be in a
queue()
if your next method chain is an animation. See his JSFiddle.之前,您传递
setTimeout
来自sale
的返回值。这传递了实际的功能。Before, you were passing
setTimeout
the return value fromsale
. This passes the actual function.