문제

I have a basic image map that executes a javascript/jquery routine when an area is clicked. At the moment, the DoStuff() method is executed twice if the area is double-clicked. Can anyone provide a method that will prevent double execution in a double-click scenario?

<img src="images/myimage.png" border="0" usemap="#Map1" />
<map name="Map1">
    <area coords="5,115,97,150" href="javascript:void(0);" onclick="DoStuff()">
    <area coords="104,116,199,149" href="javascript:void(0);" onclick="DoStuff()">
</map>
도움이 되었습니까?

해결책

Use a boolean flag.

var hasBeenClicked = false;

// Your code...

if(!hasBeenClicked){
    hasBeenClicked = true;

    // My routine

    setTimeout(function(){ // McPhelpsius's idea (wait for 1sec before we can click again)
        hasBeenClicked = false; // My routine is over, we can click again
    }, 1000);
 } else {
    // do nothing 
 }

다른 팁

If you're using jQuery you can include this at the top of the click handler. On click it will disable the element which stops any click events from happening.

function DoStuff() {
    $('[name="Map1"]').prop('disabled', true);
    //Do Stuff..
}

This explains some other options depending on your jQuery version.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top