gpt4 book ai didi

jquery - 如何在 jQuery 中销毁 $.animate 函数?

转载 作者:行者123 更新时间:2023-12-01 04:41:54 25 4
gpt4 key购买 nike

我有一些这样的代码:

function updateCounter(){
$({someValue: 2*60*1000}).animate({someValue: 0}, {
duration: 2*60*1000,
easing:"linear",
step: function() {
//dosomething
},
complete: function() {
// 2 minutes end
alert('game_over');
}
});
}

通常游戏结束会在2分钟后发出警报。但是如果我想在计数器结束之前杀死计数器怎么办?我有这样的想法:在 complete 回调函数中,检查一个变量,如果它的值为 true,则返回该函数。它对我有用,但是有什么方法可以直接删除回调函数吗?就像removeEventListener()之类的东西。

无法通过 Google 或 jQuery 网站找到它。

最佳答案

因为您的对象没有以任何方式绑定(bind)到 DOM,所以最好的方法可能是在创建对象时将其存储在变量中。这样,当您需要操作该对象时,您就可以通过变量名称来访问该对象。

function updateCounter(){
var timer = $({someValue: 2*60*1000});

timer.animate({someValue: 0}, {
duration: 2*60*1000,
easing:"linear",
step: function() {
if(yourCondition == true){ // condition for killing timer
// do stuff when your timer is going to end
// stops animation, default doesn't perform complete callback
timer.stop();
}
// normal step function events
},
complete: function() {
// 2 minutes end
alert('game_over');
}
});
}

关于jquery - 如何在 jQuery 中销毁 $.animate 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35808941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com