在开始一个会自行循环的函数之前延迟 6 秒,很小,不起作用,为什么?
我试图在 heartColor(e)
函数开始之前创建 6 秒的延迟,然后该函数将继续循环。我不明白为什么它立即启动该功能,而不是等待它应该等待的 6 秒,我做错了什么?
function heartColor(e) {
e.animate({
color: '#7ea0dd'
}, 1000).animate({
color: '#986db9'
}, 1000).animate({
color: '#9fc54e'
}, 1000, function(){
heartColor(e)
})
}
$('.something').hover(function(){
setTimeout(heartColor($(this)), 6000);
})
I'm trying to create a 6 second delay before the heartColor(e)
function begins, the function will then continue to loop. I don't understand why it's starting the function immediatley, and not waiting the 6 seconds it's supposed to, what did I do wrong?
function heartColor(e) {
e.animate({
color: '#7ea0dd'
}, 1000).animate({
color: '#986db9'
}, 1000).animate({
color: '#9fc54e'
}, 1000, function(){
heartColor(e)
})
}
$('.something').hover(function(){
setTimeout(heartColor($(this)), 6000);
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
setTimeout()
函数期望其第一个参数是函数引用(或字符串,但出于多种原因不建议这样做)。您没有向其传递函数引用,而是调用heartColor()
函数并将结果传递给setTimeout()
。因此该函数立即执行,然后六秒后什么也没有发生,因为返回值未定义,因此 setTimeout() 没有任何作用。请尝试这样做:
我包含匿名函数作为
setTimeout
的参数的原因是您对heartColor()
的调用需要传递一个参数。如果它没有任何参数,您可以这样做:请注意,
heartColor
之后没有括号 - 它获取对函数的引用而不调用它,以便稍后setTimeout
为您调用它。但是您无法同时获取对该函数的引用并提供参数,因此您需要将调用包装在另一个函数中。你可以这样做:我最初对匿名函数的回答是一种简写,并且(大多数人发现它)更方便。
我创建变量
$this
的原因是this
关键字在 JavaScript 中的工作方式,这取决于函数的调用方式。如果您只是在匿名函数(或callHeartColor()
函数)内说heartColor($(this))
this
则不会是该元素被悬停在上面。The
setTimeout()
function expects its first parameter to be a function reference (or a string, but that's not recommended for several reasons). You are not passing it a function reference, you are calling theheartColor()
function and passing the result tosetTimeout()
. So the function executes immediately, and then after six seconds nothing happens because the return value was undefined sosetTimeout()
had nothing to work with.Try this instead:
The reason I have included an anonymous function as the parameter to
setTimeout
is that your call toheartColor()
needs to pass a parameter through. If it didn't have any parameters you could do this:Note there are no parentheses after
heartColor
- that gets a reference to the function without calling it so that latersetTimeout
calls it for you. But you can't get a reference to the function and provide parameters at the same time so you need to wrap the call up in another function. You could do this:My original answer with the anonymous function is kind of short hand for that and (most people find it) more convenient.
The reason I have created a variable
$this
is because of the way thethis
keyword works in JavaScript, which depends on how a function is called. If you simply saidheartColor($(this))
inside the anonymous function (or thecallHeartColor()
function)this
would not be the element being hovered over.您正在调用函数 heartColor 而不是将其作为参数传递。你必须做:
you are invoking the function heartColor instead of passing it as a parameter. you have to do:
你想要这个:
You want this: