admin管理员组文章数量:1316817
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);
})
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);
})
Share
asked Jan 28, 2012 at 3:12
android.nickandroid.nick
11.2k24 gold badges79 silver badges112 bronze badges
3 Answers
Reset to default 9The setTimeout()
function expects its first parameter to be a function reference (or a string, but that's not remended for several reasons). You are not passing it a function reference, you are calling the heartColor()
function and passing the result to setTimeout()
. So the function executes immediately, and then after six seconds nothing happens because the return value was undefined so setTimeout()
had nothing to work with.
Try this instead:
$('.something').hover(function(){
var $this = $(this);
setTimeout(function() {
heartColor($this);
}, 6000);
})
The reason I have included an anonymous function as the parameter to setTimeout
is that your call to heartColor()
needs to pass a parameter through. If it didn't have any parameters you could do this:
setTimeout(heartColor, 6000);
Note there are no parentheses after heartColor
- that gets a reference to the function without calling it so that later setTimeout
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:
var $this = $(this);
function callHeartColor() {
heartColor($this);
}
setTimeout(callHeartColor, 6000);
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 the this
keyword works in JavaScript, which depends on how a function is called. If you simply said heartColor($(this))
inside the anonymous function (or the callHeartColor()
function) this
would not be the element being hovered over.
you are invoking the function heartColor instead of passing it as a parameter. you have to do:
$('.something').hover(function(){
setTimeout(function(){heartColor($(this))}, 6000);
})
You want this:
$('.something').hover(function(){
setTimeout(function() {heartColor($(this));}, 6000);
})
本文标签:
版权声明:本文标题:javascript - Delay 6 seconds before beginning a function that will loop itself, small, not working, why? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741349331a2373773.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论