0

I'm in process of some beginner coding on Javascript/jQuery and stuck with a problem. Long story short:

  1. I have one element on page which should ignore any clicks;
  2. Once specific function called it should wait until n clicks;
  3. Function should execute then some code;
  4. After all is done element should ignore clicks again.

I tried to play around with setInterval() / clearInterval() but did not succeed.

Please help me :)

P.S.: one way is to reload a page with the new code but it is not for my case.

UPD:

var select = function() {
    /*once called this function should enable clicks on <td>
    toggling its class to yellow and once both cells are yellow
    clicking should be disabled*/
};

$(document).ready(function(){
    $("button[name=start]").click(function(){
        select();
    });
}); 

http://jsfiddle.net/superiorbanana/Z53EU/. Hope this small bit of code will clarify the idea.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Banana
  • 23
  • 1
  • 3
  • 3
    please at least show some html – Alex Apr 23 '14 at 07:45
  • There is something called delay() in jQuery but I think you should show some code that you have tried. – Shina Apr 23 '14 at 07:45
  • Store a variable and check the variable on a click handler. If the variable is not true then don't execute the function. After it executes, set it back to false. – bozdoz Apr 23 '14 at 07:46
  • "Once specific function called it should wait until n clicks;" so when n clicks occured (on that element?), it should execute code and then ignore clicks again? – Alex Apr 23 '14 at 07:46
  • 1
    From what I understand, you can set a boolean flag to check if the specific function is called or not. You can set a counter for counting n clicks. – Ibrahim Apr 23 '14 at 07:48
  • Sorry for not providing an example. Here is the link to JSFiddle: [http://jsfiddle.net/superiorbanana/Z53EU/](http://jsfiddle.net/superiorbanana/Z53EU/). I hope it clarifies the idea... – Banana Apr 23 '14 at 08:10

4 Answers4

1

Just store the number of clicks in a variable and check it with javascript. Something like this:

var x = 0;

$('#button').click(function(){
   x++;
    if (x == 5) {
        alert("hello");
    }
});

fiddle

Senju
  • 1,035
  • 10
  • 25
1

I'm not sure whether you are looking for this,

HTML

<span class="one">click disabled</span>
<br/>
<span class="two">enable click</span>

jquery

var count=0;
$(".two").click(function(){
    $(".one").text("click enabled");
    $(".one").click(function(){
        count++;
        if(count==5)
        {
            $(".one").text("click disabled");
            $(".one").off("click");

        }
    alert("click enabled");
    });

});

Fiddle demo

In the above code, click event of first span will not fire until the second span is clicked. Click event for first span is binded only after clicking the second span. There is also a counter for click. When the counter reaches the limit click event of first span will be removed using off()

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
1

I think you're looking for jQuery's on and off methods.

Bind a click handler with on, and then turn it off when you're done with it. So, in your case, you can turn a click handler off immediately after it's been triggered. For example:

$(document).ready(function () {
    var $tds = $('td'), // store td's
        count = $tds.length; // store # of td's
    $("button[name=start]").on('click', function (e) {
        // pass `e` so that it can be used to turn itself off
        $(this).off(e); // this function won't execute again

        // bind td clicking after button's clicked
        $tds.on('click', function (e) {
            $(this).addClass('clicked').off(e); // executed once per td

            // the if statement and the count is actually 
            // not necessary; it's just to demonstrate 
            // that all the click handlers have ended.

            count--; // count - 1                
            if (count === 0) {
                console.log('there are no more click handlers');
            }
        });
    });
});

Or simply

$(document).ready(function () {
    $("button[name=start]").on('click', function (e) {
        $(this).off(e);
        $('td').on('click', function (e) {
            $(this).addClass('clicked').off(e);
        });
    });
});

Check out the code on your jsfiddle.

bozdoz
  • 12,550
  • 7
  • 67
  • 96
  • Thanks! It seems the one I looked for! Look like I did not get the idea of `off()` before :-) – Banana Apr 23 '14 at 08:45
0

You should set the event listener at the beginning to listen for any click event on that element.

Once specific function called it should wait until n clicks;

That "specific function" can set a variable to "true" that was previously false. This is what the click event listener is waiting for. Once the variable is true, a counter variable can count the number of clicks

n clicks;

Once the counter reaches the desired number of clicks you can execute whatever you desire:

if(counter === n) {
    executeMe();
    counter = 0;
}

You can use "mod" too, if the interval is always the same.

mgrstnr
  • 490
  • 1
  • 5
  • 23