2

I would like to make something that comes up after user right-clicked many times (5 clicks, for example). Then a div should be shown.

Right now I'm using this code:

document.oncontextmenu = function() { 
    $("#red").fadeIn(500).show().delay(3000).fadeOut(800);
    return false;
};

This works fine, but it misess five attempts like I have said in my request.

UPDATE: Here the right code!

document.oncontextmenu = (function() {
var counter = 0;
return function() { 
    counter++;
    if (counter == 5) {
        $("#red").fadeIn(500).show().delay(3000).fadeOut(800, function() {
        counter = 0;
    });
    }
    return false;
};

})();

Thanks to @James Thorpe

Devilix
  • 334
  • 3
  • 13

1 Answers1

5

Use what you have, but wrap it in a closure that has a variable to count how many times the inner function has been executed:

document.oncontextmenu = (function() {
    var counter = 0;
    return function() { 
        counter++;
        if (counter == 5)
            $("#red").fadeIn(500).show().delay(3000).fadeOut(800);
        return false;
    };
})();

Note that the outer function is invoked immediately to create the variable, then returns the inner function as the event handler. By using such a closure, we keep everything related to the event (including the counter variable) within the local scope without leaking anything to the global scope.

If you want to be able to invoke the div multiple times, you just need to reset the counter each time it's shown. The best place to do that is in the callback function supplied to the fadeOut function, so that the count only begins again once the div has been hidden.

if (counter == 5) {
    $("#red").fadeIn(500).show().delay(3000).fadeOut(800, function() {
        counter = 0;
    });
}
James Thorpe
  • 31,411
  • 5
  • 72
  • 93
  • @Redrif `if (counter == 5)`? – James Thorpe Jun 11 '15 at 12:33
  • i mean where is the event that is catches whether the user right-clicked? – Redrif Jun 11 '15 at 12:35
  • @Redrif That's the [`contextmenu` event](https://developer.mozilla.org/en-US/docs/Web/Events/contextmenu) that the OP is already binding to – James Thorpe Jun 11 '15 at 12:36
  • @JamesThorpe, why do you self-execute the function you set to oncontextmenu? – AmmarCSE Jun 11 '15 at 12:36
  • oh, I see, for the counter. Nice! – AmmarCSE Jun 11 '15 at 12:37
  • 2
    @AmmarCSE I'm not, I'm self executing the outer function, which creates a local scope for the `counter` variable, and _returns the inner function_ to use as the event handler – James Thorpe Jun 11 '15 at 12:37
  • @JamesThorpe Thanks, just tested your code... it works, but after the div go away, i will try to make other five clicks..but the div doesn't comes up no more... Its normal? – Devilix Jun 11 '15 at 13:00
  • @Devilix You'd need to reset the counter if you want it able to show again. I'll edit my answer to reflect that – James Thorpe Jun 11 '15 at 13:01
  • @JamesThorpe i have add counter = 0; but now the div just not appear in any way... :( Maybe there is something wrong EDIT. My mistake... i was missing { } – Devilix Jun 11 '15 at 13:10
  • Did you notice I changed the `if` to include braces (`{}`) since it now has a two statement body? If the `counter = 0;` isn't included as part of the if statement, it will be resetting back to 0 every time you right click – James Thorpe Jun 11 '15 at 13:13
  • There is a "memory effect" if i click repeatedly... Can we make something to stop this inconvenient? I'm sorry, but i'm a beginner... :( – Devilix Jun 11 '15 at 13:17
  • @JamesThorpe i'm talking about my comment of 30min ago... There is a "memory effect" if i click repeatedly... and you have answered that "I think I know what you mean, I'll edit my answer again..." – Devilix Jun 11 '15 at 13:54
  • @Devilix Yes - I edited again after that. The counter is now being reset in the callback of `fadeout` - ie not until after the div has been hidden. Any clicks that happen while it's open won't count – James Thorpe Jun 11 '15 at 13:56
  • @JamesThorpe Hi, i need your help again... Did you can? – Devilix Jun 13 '15 at 15:47
  • If you have a new question... Ask a new question :) more people will see it and be able to help, this site is VERY active – James Thorpe Jun 13 '15 at 15:48
  • But is related to this code! If is possible re-enable right-click on the – Devilix Jun 13 '15 at 17:31
  • You've not even mentioned a textarea until now - that very much sounds like a _different_ question - it may be related, but it's not the same issue! – James Thorpe Jun 13 '15 at 17:54
  • @JamesThorpe Yes, i have a textarea where there is a html code for a banner... and I had not thought about this inconvenience :( sorry! There is a simple solution? Or you suggest to open a new question? – Devilix Jun 13 '15 at 18:04
  • @JamesThorpe if you can help me here... please! http://stackoverflow.com/questions/30822486/how-to-re-enable-rightclick-only-for-specific-tag-id – Devilix Jun 14 '15 at 00:36