avr39-ripe

jsTickerTHISproblem

Mar 31st, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Ticker(tickerId, baseDelay=3000, ticksCount=10)
  2. {
  3.     //let this = {};
  4.     this.tickerId = tickerId;
  5.     this.delay = baseDelay;
  6.     this.ticksCount = ticksCount;
  7.     this.cnt = 0;
  8.     this.id;
  9.     this.arm = function()
  10.     {
  11.         this.cnt = 0;
  12.         this.id = setTimeout(function ticker(){
  13.             console.log(`Ticker #${this.tickerId} ${this.cnt}`);
  14.             (this.cnt++) % 2 ? this.delay *= 2 : this.delay /= 2;
  15.             if (this.cnt == this.ticksCount && this.ticksCount != -1) { clearTimeout(this.id); }
  16.             else { this.id = setTimeout(ticker, this.delay)};
  17.         },this.delay);
  18.     };
  19.     this.disarm = function()
  20.     {
  21.         clearTimeout(this.id);
  22.     };
  23.     this.set = function(baseDelay=3000, ticksCount=10)
  24.     {
  25.         this.delay = baseDelay;
  26.         this.ticksCount = ticksCount;
  27.     };
  28.    
  29.     this.get = function()
  30.     {
  31.         console.log(`Ticker #${this.tickerId}\nDelay: ${this.delay}\nTicksCount: ${this.ticksCount}`);
  32.     }
  33.     //return this;
  34. };
Add Comment
Please, Sign In to add comment