防止Javascript被执行两次 [英] Prevent Javascript from being executed twice

查看:76
本文介绍了防止Javascript被执行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在开发的脚本,它创建了一个滑动按钮类型效果。五个div位于彼此旁边,每个链接都有一个链接。当点击其中一个DIVS时,关联的内容会被展开,其余部分将关闭。

I have a script that I am developing that creates a sliding button type effect. Five div's are situated next to eachother each with a link. When one of those DIVS are clicked on the associated content is expanded and the rest of the Div's are closed.

问题是,如果用户点击Div两次加载或点击另一个快速连续的Div,裂缝开始显示。

The problem is, if a user clicks the Div twice while it loads or clicks another Div in rapid succession, cracks start to show.

我想知道是否可以只允许执行一次查询并等待完成而不是排队。

I am wondering if it would be possible to only allow the query to be executed once and wait until completion rather than queuing it.

下面是我当前的代码,如果它是垃圾可以随意评论我如何可以改善它...我不是最好的Javascript / jQuery:P

Here is my current code, if it is crap feel free to comment on how I could better it... I am not the best at Javascript/jQuery :P

    function mnuClick(x){
    //Reset all elements
    if($('#'+x).width()!=369){
        $('.menu .menu_Graphic').fadeOut(300);
        $('.menu_left .menu_Graphic').fadeOut(300);
        $('.menu_right .menu_Graphic').fadeOut(300);
        $('.menu').animate({width: "76px"},500);
        $('.menu_left').animate({width: "76px"},500);
        $('.menu_right').animate({width: "76px"},500);
    }
        var ElementId = '#' + x;

        $(ElementId).animate({
            width: 369 + "px"
        },500, function(){ 
            $(ElementId + ' .menu_Graphic').fadeIn(300);
        });
}

预先感谢,
Chris。

Thanks in advance, Chris.

推荐答案

您需要一个isRunning标志。在开始之前检查它。在开始序列时设置它,当它结束时清除它。

You need a "isRunning" flag. Check for it before you start. Set it when you start the sequence, clear it when it ends.

这篇关于防止Javascript被执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆