1

I have a global variable

var trigger = 0;

In addition I have some functions which once completed,they increase the value of this variable.

My goal is to trigger an additional function once the variable trigger has reached the value 2.

Which is the best way to achieve this ?

S tz
  • 27
  • 4
  • 1
    Where is the function that is incrementing or decrementing the value trigger? – Jabari Dash Jun 18 '18 at 18:53
  • just do a check inside the function that increment/decrement, something like: `trigger++; if (trigger == 2){//call function here}` – Calvin Nunes Jun 18 '18 at 18:55
  • 1
    you can see many sample [here](https://stackoverflow.com/questions/1759987/listening-for-variable-changes-in-javascript) to achieve the same – jagad89 Jun 18 '18 at 18:56

2 Answers2

1

One possible option is just to place an interval watch on the trigger.

var trigger = 0;
var intervalID = window.setInterval(triggerWatch, 500);

function triggerWatch() {
  if(trigger > 20){
    // Do Stuff Here
  }
}
dgig
  • 3,977
  • 2
  • 30
  • 44
0

Does this give you some insight into how you might implement your functions that increment the trigger?

var trigger = 0;

var sampleIncrementingFunction = function() {
  trigger++;
  if (trigger > 3) {
    conditionalFunction();
  }
}

var conditionalFunction = function() {
  console.log('trigger is now greater than three');
}

sampleIncrementingFunction();
sampleIncrementingFunction();
sampleIncrementingFunction();
sampleIncrementingFunction();
LexJacobs
  • 2,195
  • 13
  • 21