javascript - Detect change in timer variable using jQuery -


i have timers updating every second. want execute function on change of timer variable. if can me out. have provided code below.

for(var i=0; i<listingtime.length; i++){     if (listingtime[i].time >= 0) {       var second = listingtime[i].time / 1000;       var currenthour = parseint(second / 3600);     } } 

here, want detect change of currenthour variable.

you can set previous value variable prior loop, , whenever updating currenthour, match previousvalue variable. if same, not changes, if not same, changed , can execute callback.

one more other way use object prototypes changes value of currenthour. following code shows that:

above code can modified following add changelistner

var hourlistner = {    currenthour: null,    update:  function(newvalue, callback){            if(this.currenthour !== newvalue){        this.currenthour = newvalue;        callback(newvalue);      }    }  }  timer = object.create(hourlistner);    var changelistner = function(value){     //value new value of currenthour    console.log(value)  }    for(var i=0; i<listingtime.length; i++){      if (listingtime[i].time >= 0) {        var second = listingtime[i].time / 1000;        var currenthour = parseint(second / 3600);        timer.update(currenthour, changelistner)      }  }

and trick can tested independently in following code:

var hourlistner = {    currenthour: null,    update: function(newvalue, callback) {        if (this.currenthour !== newvalue) {        this.currenthour = newvalue;        callback(newvalue);      }    }  }  timer = object.create(hourlistner);    var changelistner = function(value) {    //value new value of currenthour    console.log(value)    $('body').append('<p>'+value+'</p>')  }  var interval = setinterval(function(){    var t = new date();    timer.update(t.getminutes(), changelistner)  }, 200)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

this execute changelistner if there change previous value.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -