setTimeout后的jquery函数

发布于 2024-09-13 13:48:27 字数 674 浏览 5 评论 0原文

我有一个下拉菜单,其中包含以下内容:

$(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 技术交流群。

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

发布评论

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

评论(3

橘亓 2024-09-20 13:48:27

我认为你正在尝试做这样的事情:

尝试一下: http://jsfiddle .net/YFPey/

var opened = false;
var timeout;
$("#menu_tab").click(function() {
      // If there's a setTimeout running, clear it.
    if(timeout) {
        clearTimeout(timeout);
        timeout = null;
    }
    if(opened) {
        $("#menu_box").animate({"top": "+=83px"}, "slow");
    } else {
        $("#menu_box").animate({"top": "-=83px"}, "slow");
             // Set a timeout to trigger a click that will drop it back down
        timeout = setTimeout(function() {
            timeout = null;
            $("#menu_tab").click();
        }, 2000);
    }
    $("#menu_content").slideToggle("slow");
    $("#menu_tab .close").toggle();
    opened = !opened;
});​

I think you are trying to do something like this:

Try it out: http://jsfiddle.net/YFPey/

var opened = false;
var timeout;
$("#menu_tab").click(function() {
      // If there's a setTimeout running, clear it.
    if(timeout) {
        clearTimeout(timeout);
        timeout = null;
    }
    if(opened) {
        $("#menu_box").animate({"top": "+=83px"}, "slow");
    } else {
        $("#menu_box").animate({"top": "-=83px"}, "slow");
             // Set a timeout to trigger a click that will drop it back down
        timeout = setTimeout(function() {
            timeout = null;
            $("#menu_tab").click();
        }, 2000);
    }
    $("#menu_content").slideToggle("slow");
    $("#menu_tab .close").toggle();
    opened = !opened;
});​
窗影残 2024-09-20 13:48:27

您在这里使用的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.

べ映画 2024-09-20 13:48:27

有两件事对我来说很突出:

  1. 因为 opened 开始时为 false,因此计时器仅在第二次单击时启动。
  2. 在计时器处理程序中,您是否应该更新opened

Two things that stand out to me:

  1. Because opened starts out as false, the timer only gets started on the second click.
  2. In the timer handler, should you be updating opened.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文