javascript - window.setInterval calls the referenced function only once and stops (from within a type = specific implementation) -


i'm experimenting timer (javascript type). use interval instead of timeout. therefore made timer type 3 parameters: duration in milliseconds, interval in milliseconds , jquery object attached timer. within inittimerplugin() function create jquery object, not added dom-structure yet (just try if events try raise, work), initialize global timer of timer type, define duration of 30 seconds, interval of 1 decisecond , attach jquery timer timer. call start function (which function of timer type). sets interval , calls tick function , expect tick long total duration not equal 0. problem is, ticks once.

i know there lots of questions , answers intervals in stackoverflow, none of them use type implement timer, , cannot find solution problem. explain what's reason timer doesn't keep tick-ing.

var timer;  var timer = function (ms, interval, $timer) {     this.ms = ms;     this.interval = interval;     this.$timer = $timer;     this.counter = null;       this.start = function () {         this.counter = window.setinterval(this.tick(), this.interval);     };      this.tick = function () {         this.ms -= this.interval;         console.log(this.ms);         this.trigger('tick');         console.log("ticking...");         if (this.ms < 0) {             this.ms = 0;         }         if (this.ms == 0) {             window.clearinterval(this.counter);         }     };      this.hold = function () {      };      this.expire = function () {      };      this.trigger = function (event) {         if (typeof event === 'string') {             var e = jquery.event(event);             this.trigger(e);         } else {             this.$timer.trigger(event);         }     } };  var inittimerplugin = function () {     var $timer = $('<div class="timer">');     timer = new timer(30000, 100, $timer);     $timer.on('tick', function () {         console.log('hello world');     });     timer.start();  };  $(document).ready(inittimerplugin); 

fyi: jquery version use 3.2.1.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -