setTimeout后的jquery函数
我有一个下拉菜单,其中包含以下内容:
$(document).ready(function(){
var opened = false;
$("#menu_tab").click(function(){
if(opened){
$("#menu_box").animate({"top": "+=83px"}, "slow");
setTimeout(function(){
$("#menu_box").animate({"top": "+=83px"}, "slow");
}, 2000);
clearTimeout();
}else{
$("#menu_box").animate({"top": "-=83px"}, "slow");
}
$("#menu_content").slideToggle("slow");
$("#menu_tab .close").toggle();
opened = opened ? false : true;
});
});
因此,单击 menu_tab 后,菜单会下降并保持不变,直到再次单击,但我想要一个超时,以便在 2 秒后菜单再次下降。
我显然编码错误,因为超时不起作用。任何帮助将不胜感激! TIA。
I have a dropUp menu with the following:
$(document).ready(function(){
var opened = false;
$("#menu_tab").click(function(){
if(opened){
$("#menu_box").animate({"top": "+=83px"}, "slow");
setTimeout(function(){
$("#menu_box").animate({"top": "+=83px"}, "slow");
}, 2000);
clearTimeout();
}else{
$("#menu_box").animate({"top": "-=83px"}, "slow");
}
$("#menu_content").slideToggle("slow");
$("#menu_tab .close").toggle();
opened = opened ? false : true;
});
});
So after clicking on the menu_tab, the menu drops up and stays up until clicked again, but I'd like a timeout so that after say 2 seconds the menu drops down again.
I've obviously got the coding wrong because the timeout isn't working. Any help would be appreciated! TIA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你正在尝试做这样的事情:
尝试一下: http://jsfiddle .net/YFPey/
I think you are trying to do something like this:
Try it out: http://jsfiddle.net/YFPey/
您在这里使用的clearTimeout()是错误的。您需要向其传递一个对使用 setTimeout() 创建该计时器时返回的 ID 的引用。
但不能说这是否导致了您的问题(可能不是)。如果您在 Javascript 错误控制台中得到任何可能有帮助的信息。
Your use of clearTimeout() here is wrong. You need to pass it a reference to the ID returned when you created that timer with setTimeout().
Can't say if that's causing your problem though (it probably isn't). If you get anything in the Javascript error console that might help.
有两件事对我来说很突出:
opened
开始时为 false,因此计时器仅在第二次单击时启动。opened
。Two things that stand out to me:
opened
starts out as false, the timer only gets started on the second click.opened
.