Advertisement
avr39-ripe

tickerClassDraft

Apr 6th, 2020
273
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.     console.log("Ticker works hard with new! :)");
  5.     this.tickerId = tickerId;
  6.     this.delay = baseDelay;
  7.     this.ticksCount = ticksCount;
  8.     this.cnt = 0;
  9.     this.id;
  10.     Ticker.prototype.parentProperty = 42;
  11.     Ticker.prototype.tickerArr = () => {
  12.             console.log(`Ticker #${this.tickerId} ${this.cnt}`);
  13.             (this.cnt++) % 2 ? this.delay *= 2 : this.delay /= 2;
  14.             if (this.cnt == this.ticksCount && this.ticksCount != -1) { clearTimeout(this.id); }
  15.             else { this.id = setTimeout(this.tickerArr, this.delay)};
  16.     };
  17.     Ticker.prototype.armArr = function()
  18.     {
  19.         this.cnt = 0;
  20.         this.id = setTimeout(this.tickerArr, this.delay);
  21.     }
  22.     Ticker.prototype.arm = function()
  23.     {
  24.         this.cnt = 0;
  25.         this.id = setTimeout(function ticker(obj){
  26.             console.log(`Ticker #${obj.tickerId} ${obj.cnt}`);
  27.             (obj.cnt++) % 2 ? obj.delay *= 2 : obj.delay /= 2;
  28.             if (obj.cnt == obj.ticksCount && obj.ticksCount != -1) { clearTimeout(obj.id); }
  29.             else { obj.id = setTimeout(ticker, obj.delay,obj)};
  30.         },this.delay,this);
  31.     };
  32.     Ticker.prototype.disarm = function()
  33.     {
  34.         clearTimeout(this.id);
  35.     };
  36.     Ticker.prototype.set = function(baseDelay=3000, ticksCount=10)
  37.     {
  38.         this.delay = baseDelay;
  39.         this.ticksCount = ticksCount;
  40.     };
  41.    
  42.     Ticker.prototype.get = function()
  43.     {
  44.         console.log(`Ticker #${this.tickerId}\nDelay: ${this.delay}\nTicksCount: ${this.ticksCount}`);
  45.     }
  46.     //Object.setPrototypeOf(this,Ticker.prototype)
  47.     //return this;
  48. };
  49.  
  50. class Ticker
  51. {
  52.     constructor(tickerId, baseDelay=3000, ticksCount=10)
  53.     {
  54.         this.tickerId = tickerId;
  55.         this.delay = baseDelay;
  56.         this.ticksCount = ticksCount;
  57.         this.cnt = 0;
  58.         this.id = 0;
  59.  
  60.         Ticker.prototype.tickerArr = () => {
  61.             console.log(`Ticker #${this.tickerId} ${this.cnt}`);
  62.             (this.cnt++) % 2 ? this.delay *= 2 : this.delay /= 2;
  63.             if (this.cnt == this.ticksCount && this.ticksCount != -1) { clearTimeout(this.id); }
  64.             else { this.id = setTimeout(this.tickerArr, this.delay)};
  65.         };
  66.     }
  67.     armArr()
  68.     {
  69.         this.cnt = 0;
  70.         this.id = setTimeout(this.tickerArr, this.delay);
  71.     }
  72.     arm()
  73.     {
  74.         this.cnt = 0;
  75.         this.id = setTimeout(function ticker(obj){
  76.             console.log(`Ticker #${obj.tickerId} ${obj.cnt}`);
  77.             (obj.cnt++) % 2 ? obj.delay *= 2 : obj.delay /= 2;
  78.             if (obj.cnt == obj.ticksCount && obj.ticksCount != -1) { clearTimeout(obj.id); }
  79.             else { obj.id = setTimeout(ticker, obj.delay,obj)};
  80.         },this.delay,this);
  81.     };
  82.    
  83.     disarm()
  84.     {
  85.         clearTimeout(this.id);
  86.     };
  87.    
  88.     set(baseDelay=3000, ticksCount=10)
  89.     {
  90.         this.delay = baseDelay;
  91.         this.ticksCount = ticksCount;
  92.     };
  93.    
  94.     get()
  95.     {
  96.         console.log(`Ticker #${this.tickerId}\nDelay: ${this.delay}\nTicksCount: ${this.ticksCount}`);
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement