jQuery Issue, the event is called multiple time

This also shows how you can call jQuery unbind method.

If your function is being called multiple time when you are attaching a event using jQuery then you can use jquery unbind method.

I was facing same issue, i attached a function call to mouseup event using jquery as below

function setupContextMenuCall(){
$(sender).mouseup(function(e) {
     if (e.which == 3) {
        setConditionalMenu();
     }
}
}

I called above code during the rendering event of my data bound table on client side. whenever there was a change or action like mark as favorite, i have to refresh this table with the updated data. And this was causing a call to above function again and was attaching the click event again though it was already present.
Due to same my context menu open time was increasing after each refresh of data bound table.

to solve this issue i just called unbind method of jQuery as below

function setupContextMenuCall(){
$(sender).unbind(‘mousedown’);
$(sender).mouseup(function(e) {
     if (e.which == 3) {
        setConditionalMenu();
     }
}
}

This sounds stupid but this has solved my problem.

Tarun Juneja

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s