David Folksman
David Folksman

Reputation: 235

How do you stop this Jquery mousedown event from firing after the first event

I want to fire an event when an image is clicked on but only once, IE disable the functionality after the first click. I tried..

var img = new Kinetic.Image({
                    x: position[rand][0],
                    y: position[rand][1],
                    image: imageObj,
                    width: 106,
                    height: 118
                });

img.on('mousedown', function(e) {

   do stuff

   e.stopPropagation();
}

and

img.on('mousedown', function() {

       do stuff

       return false;
}

any ideas?

Upvotes: 1

Views: 2859

Answers (3)

Gibolt
Gibolt

Reputation: 47257

Use modern Js event, with "once"!

img.addEventListener("mousedown", function() {
    // Do something
}, {once : true});
// You might also consider adding "capture : true" to replace stopPropogation

Documentation, CanIUse

Upvotes: 1

Nguyễn Linh
Nguyễn Linh

Reputation: 5

img.onclick = function() {
 do stuff
}

this will work :)

Upvotes: 0

PSL
PSL

Reputation: 123739

Use one instead of on to register the event just once.

img.one('mousedown', function(e) {

   do stuff

   e.stopPropagation();
}

You can also use off to turn off the event inside the on handler.

img.on('mousedown', function(e) {

   $(this).off('mousedown'); //turn off the handler
   do stuff

}

Upvotes: 2

Related Questions