javascript - setInterval doesn't get cleared, function keeps getting executed -


i have following function:

function monitorclimate() {      var sensorreadinginterval;      function startclimatemonitoring(interval) {          sensorreadinginterval = setinterval(function() {              io.emit('sensorreading', {                 temperature: sensor.gettemp() + 'c',                 humidity: sensor.gethumidity() + '%'             });          }, interval);          console.log('climate control started!');      }      function stopclimatemonitoring() {         clearinterval(sensorreadinginterval);         console.log('climate control stopped!');     }       return {         start: startclimatemonitoring,         stop: stopclimatemonitoring     };  } 

i watching button changes of state this:

button.watch(function(err, value) {     led.writesync(value);      if (value == 1) {         monitorclimate().start(1000);     } else {         monitorclimate().stop();     }  }); 

the problem after monitorclimate().stop() call, setinterval keeps getting triggered, socketio keeps on emitting sensorreading event.

what doing wrong here?

every time call monitorclimate() creating new set of functions monitorclimate().start() , monitorclimate().stop() not working on same interval. try like:

var monitor = monitorclimate(); button.watch(function(err, value) {     led.writesync(value);      if (value == 1) {         monitor.start(1000);     } else {         monitor.stop();     } }); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -