Stopping a function after IF condition is reached

ladanta

I have a button that on each click, adds 1 to a variable. When this variable goes above 5, an alert is triggered. However, after this the trigger is still constantly activated. I have tried using == for the checks rather then > , but it does the same thing. Any ideas?

http://jsfiddle.net/1345qam8/

// Declare the variable
  var generic_counter = 0;
  // Log the variable
  console.log(generic_counter);    
  // When button is clicked, add 1 to the variable
  $(document).ready(function() {
    $(".generic").click(function(add_generic) {
      if (generic_counter > 5) {
        alert("You've been looking up a lot about GENERIC, maybe contact this group?")
      } else {
        generic_counter += 1;
        console.log(generic_counter);
      }
    });
  });
Guffa

If you use == (or ===) for the comparison, and always increase the counter, the alert only comes at the sixth click.

Note: Declare the variable inside the ready event handler, it doesn't need to be a global variable.

// When button is clicked, add 1 to the variable
$(document).ready(function() {

  // Declare the variable
  var generic_counter = 0;
  // Log the variable
  console.log(generic_counter);    

  $(".generic").click(function() {
    generic_counter++;
    console.log(generic_counter);
    if (generic_counter == 6) {
      alert("You've been looking up a lot about GENERIC, maybe contact this group?");
    }
  });

});

You can also unregister the event handler using the off method once the altert has been shown, if you don't want to continue counting:

// When button is clicked, add 1 to the variable
$(document).ready(function() {

  // Declare the variable
  var generic_counter = 0;
  // Log the variable
  console.log(generic_counter);    

  $(".generic").click(function() {
    generic_counter++;
    console.log(generic_counter);
    if (generic_counter == 6) {
      alert("You've been looking up a lot about GENERIC, maybe contact this group?");
      $(".generic").off('click');
    }
  });

});

For a more generic solution you can make a plugin:

$.fn.clickUntil(cnt, message) {
  var elements = this;
  return this.click(function(){
    if (--cnt == 0) {
      alert(message);
      elements.off('click');
    }
  });
}

Then you can easily make messages for several elements. Example:

$(document).ready(function() {
  var items = [
    { selector: '.generic', name 'GENERIC' },
    { selector: '.super', name 'SuPeR' },
    { selector: '.mordor', name 'M0rd0r' }
  ];
  $.each(items, function(i, o){
    $(o.selector).clickUntil(6, 'You've been looking up a lot about ' + o.name + ', maybe contact this group?');
  });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

setInterval function not stopping when array length is reached

If statement not stopping after condition is met

Stopping loop after condition is met Java

Making condition persistent after limit is reached

Stopping a JavaScript function when a certain condition is met

Stopping synchronous function after 2 seconds

Is there a way of stopping a function after a delay? Nodejs

Stopping a function after a timer has stopped

Call another function after timeout is reached

While loop not stopping after condition is met (bash/linux)

changing the condition for a loop after a maximum value is reached for the first time

Lambda Expression returning a bool flag not stopping condition variables wait() function

Why is my program stopping after the System.in.read() function?

Function not looping through all the values and stopping after first value in the loop

Run a function after a variable has reached a predifined value

Timer stopping after 0.29

Stopping Vbscript After Completion

jQuery stopping after parseJSON

Declaring a function so that one of its arguments determines the stopping condition of a while loop (R)

How can I map a function to a list and stop when a condition is fulfilled and tell me if it stopped or reached the end?

For loop ends before condition is reached

find siblings until condition is reached

Why is this recursive function not stopping?

setInterval function not stopping

Recursive function not stopping?

Stopping the update function in Unity

Javascript function not stopping submit

Return not stopping function in Update()

Stopping function processing externally