Jasminder Pal Singh
Jasminder Pal Singh

Reputation: 512

How to disable mouseover event with javascript/jquery?

In my code a function execute when user mouseover on a div for first time and the function take like 30 seconds to complete, during that 30 seconds if user mouseover the same div again , the function executes again so i want to disable mouseover event for the second time. Is there any javascript or jquery code to disable the even from that div? THanks , any help will be heartly appreciated.

Upvotes: 1

Views: 18704

Answers (3)

adeneo
adeneo

Reputation: 318162

Use jQuery's on() and off() with some sort of callback, for example :

$("#myElementID").on('mouseover', myFunction);

myFunction(e) {
    var myElement = e.target;
    myElement.off('mouseover', myFunction);
    //do something that takes 30 seconds
    myElement.animate({top: 1000}, 30000, function() { //callback
        myElement.on('mouseover', myFunction);
    });
}

Upvotes: 10

Sujit Agarwal
Sujit Agarwal

Reputation: 12508

You can have a look at an interesting feature of jquery ... .one() here is the link for it

Description: Attach a handler to an event for the elements. The handler is executed at most once per element.

2nd idea

I can give you one more idea, suppose when the mouse over is occurred once, just unbind the mouseover event before starting your operations, then as soon as the operations are over, bind them back again...

Upvotes: 3

nemo
nemo

Reputation: 1685

var isExecuting = false;

function yourThirtySecFunction() {
  if (isExecuting == true) return;
  isExecuting = true;
  //do your stuff here

  isExecuting = false;
}

Upvotes: 3

Related Questions