javascript setTimeout 或 jquery 延迟 - 两者都不适合我

发布于 2024-10-04 11:13:39 字数 541 浏览 6 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

背叛残局 2024-10-11 11:13:40

在你的第一个解决方案中,似乎 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 of sale(), which is undefined.

淡淡的优雅 2024-10-11 11:13:40
setTimeout("sale();", 3000);
setTimeout("sale();", 3000);
烟织青萝梦 2024-10-11 11:13:39

如果是 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);

In case of setTimeout you're simply doing it wrong.

setTimeout(sale(), 3000); // will call sale and use the RETURN value in the callback but sale returns undefined

You need to pass in a function:

function sale() {
    $('#sale').slideDown(500);
}

setTimeout(sale, 3000); // just pass in the reference to sale()

Other possibility:

// 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) 

And last but not least:

setTimeout(function() { $('#sale').slideDown(500); }, 3000);
罪歌 2024-10-11 11:13:39

您需要排队才能让 delay() 工作。

$('#sale').queue(function() {

   $(this).delay(3000).slideDown(500).dequeue();

});

查看它。

Patrick Dw 在评论中告知,如果您的下一个方法链是动画,则不需要位于 queue() 中。请参阅他的 JSFiddle

You need to be in a queue for delay() to work.

$('#sale').queue(function() {

   $(this).delay(3000).slideDown(500).dequeue();

});

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.

怂人 2024-10-11 11:13:39
setTimeout(sale, 3000);

之前,您传递 setTimeout 来自 sale 的返回值。这传递了实际的功能。

setTimeout(sale, 3000);

Before, you were passing setTimeout the return value from sale. This passes the actual function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文