2

I have an array of anchors, I am binding an event to each one:

$j('.opts a').live("click", function(e){}) ;

I would like to stop the other anchors from being clickable until this click event has finished.

$j('.opts a').live("click", function(e){
  $j('.opts a').unbind('click'); 
  //do something
  $j('.opts a').bind('click');
});

This is just an example of one thing i've tried, but even the unbind doesn't work.

2
  • @JamesMcLaughlin: But the user isn't single-threaded, so she can fire events in parallel with the mouse. Commented Aug 8, 2012 at 14:13
  • @AaronDigulla You're right, never mind. Commented Aug 8, 2012 at 14:18

3 Answers 3

2

You can't really keep a link from being clickable. You can only unbind whatever JS event you've registered. You'll want to make sure to call preventDefault(), otherwise a link will just follow its href.

Try something like this.

var isDoingSomething = false;
$j('.opts a').on('click', function (e) {
    e.preventDefault();
    if (!isDoingSomething) {
        isDoingSomething = true;
        // do something
        isDoingSomething = false;
    }
});

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

exactly what I was going to suggest, including the .on - but probably need to bind the .on to the direct parent of the .opts for best performance. (whatever that parent is)
2

I don't think jQuery is the correct tool here. Your problem is that the events queue while your JavaScript does something.

So what you really need is something like "swallow all queued click events when I return."

I suggest to move the rebinding of the click handlers into a new function and call that in a timer. That allows the browser to process the queued events.

And maybe unbinding is the wrong approach as well. If you're always using the same handler, turn it into an object with a skip property:

skip: false,
run: function( e ) {
    if (this.skip) return;

    this.skip = true;

    ... do something ...

    var self = this;
    setTimeout( function() { self.skip = false; }, 50 );
}

[EDIT] Another alternative: If all the links are in one HTML element (like a parent div or something), you can install a click handler for that element which does nothing. That would also swallow the click events. You will still need the timeout handler to make sure the events are processed after you return from your function.

1 Comment

+1 for reminding us that not all JavaScript problems are jQuery problems.
0

Bad ->

$j('.opts a').live("click", function(e){
  $j('.opts a').unbind('click'); 
  //do something
  $j('.opts a').bind('click');
});

Good ->

$j(document).on("click", '.opts a', function(e){
  $j('.opts a').off('click'); 
  //do something
  $j('.opts a').on('click', //a function such as 'myClickFunction' referencing what you want to occur.);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.