0

Iv been struggling with this for a while and it seems like an important thing to be able to do with JQuery.. i need only one instance of this function to be able to run at any given time - to stop it messing up if you spam the click...

So The Questions: is there a convenient JQuery method to only run one instance of a function at a time? - if not their totally should be!!

or

am i missing some logic needed to make the variable method work?(see below.)

I have tried using a a variable methods similar to:

var wait = false;    //global variable
//onclick() function starts
if(wait == true){
return false;
}
wait = true;
//function runs
 wait = false;     //resets wait to false at the end so it can run again
//onclick() function finishes    

but this doesn't work,- I think the callback functions in my click function mess it up somehow....? original JQuery code below: (html and css can be provided as well if it helps)

$('#findUs').click(function(){

    if(($('.findUs').length)&&($('.infoDropdown').is(':visible'))){  //---.length to check if class 'findUs' exists-----

            $('.infoDropdown').slideToggle("slow",function(){
                $('#findUs').attr('class','contactTitle',function(){
                    $('.findUs').remove(function(){
                        return false;

                        });
                    });
                });
    }

    $('.infoDropdown').load('global/forms.php .findUs',{},function(){

                $('#findUs').attr('class','selectedContact');
                $('#phoneUs').attr('class','contactTitle');
                $('#faxUs').attr('class','contactTitle');

        if($('.infoDropdown').is(':hidden')){
                $('.infoDropdown').slideToggle("slow");
            }
            });
        });
bodovix
  • 299
  • 2
  • 4
  • 11
  • Possibly helpful ---> http://api.jquery.com/one/ – Scott Nov 21 '16 at 10:44
  • I tried one() , i need this to be able to run multiple times but only one at a time, – bodovix Nov 21 '16 at 10:48
  • 1
    The problem with this, is that your JQuery function is asynchronous. Possible solution is to [make your function synchronous](http://stackoverflow.com/a/27012831/6501094) – Roy123 Nov 21 '16 at 10:50
  • if you use inline function for onclick attribute in your html element you can call it every time you want! – farhadamjady Nov 21 '16 at 11:01
  • 1
    If "#findus" is a button element you could disable it until you are done processing. I believe on buttons the click event doesn't fire if it is disabled. Not sure about other element types. – Adrian Nov 21 '16 at 11:52
  • #findUs is a div currently, i could probably try it as a button and just abuse the css to make it look right if this way fails, but I'm going to try exhaust the 'make your function synchronous' suggestion first as it would make a more all encompassing solution,, – bodovix Nov 21 '16 at 12:44

0 Answers0