Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Study</title>
- </head>
- <body>
- <script>
- `use strict`
- function Ticker(tickerId, baseDelay = 3000, ticksCount = 10, onTimeout = undefined ) {
- this.tickerId = tickerId;
- this.delay = baseDelay;
- this.ticksCount = ticksCount;
- this.cnt = 0;
- this.arm = function () {
- this.cnt = 0;
- this.id = setTimeout(function ticker(object) {
- console.log(`Ticker #${object.tickerId} ${object.cnt}`);
- (object.cnt++) % 2 ? object.delay *= 2 : object.delay /= 2;
- if (object.cnt == object.ticksCount && object.ticksCount != -1)
- {
- clearTimeout(object.id);
- if (object.onTimeout) { object.onTimeout();}
- }
- else { object.id = setTimeout(ticker, object.delay, object); };
- }, this.delay, this);
- return this;
- }
- this.disarm = function () {
- clearTimeout(this.id);
- return this;
- }
- this.set = function (baseDelay = 3000, ticksCount = 10) {
- this.delay = baseDelay;
- this.ticksCount = ticksCount;
- return this;
- }
- this.setOntimeout= function(onTimeout)
- {
- this.onTimeout = onTimeout;
- }
- }
- {
- let t1 = new Ticker(26);
- let t2 = new Ticker(42);
- t2.setOntimeout(() => { console.log('t2 finished ticking!') });
- t2.set(1000, 8).arm();
- t1.set(2500, 20).arm();
- setTimeout(() => { t1.disarm() }, 8000);
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement