Skip to content

Watch a state

Anil Maharjan edited this page Dec 16, 2020 · 7 revisions

Watch gets newValue, oldValue and name of the state and is invoked everytime the state changes.

function countWatcher(newVal, oldVal, stateName) {
  // Do something when value of count state changes
}

Example

function countWatcher(newVal, oldVal) {
	if (newVal > 100) {
		alert("The counter value has exceeded 100!");
	}

	if (newVal < 0) {
		alert("The counter value has gone negative!");
	}

	if (oldVal === 50 && newVal === 51) {
		alert("You are half way there.");
	}
}

The above example alerts user whenever the counter value has changed to certain values. You can use oldValue and or newValue.


Read more about How to handle multiple Comfey Apps →