From e1136f34e65c19ea97cb7712f09173358ce8b35c Mon Sep 17 00:00:00 2001 From: Nadi Date: Sun, 26 Oct 2014 00:48:59 -0700 Subject: [PATCH 1/3] Issue #10: Random delay What do you think, if Delay would be an array? If there is 1 number in the array, the delay will be equal to this number. If there are two numbers in the array, the delay would get random number in between. --- assets/js/tick.js | 10 +++++++++- demo/assets/js/interface.js | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/assets/js/tick.js b/assets/js/tick.js index 78a9564..66ea04c 100755 --- a/assets/js/tick.js +++ b/assets/js/tick.js @@ -142,10 +142,18 @@ Tick.prototype.set_timer = function() { var _this = this; + var _delay; if (this.running) { + if (this.options.delay.length == 1) { + _delay = this.options.delay[0]; + } else if (this.options.delay.length == 2) { + _delay = Math.floor( Math.random() * ( this.options.delay[1] - this.options.delay[0] + 1) + this.options.delay[0] ); + } else { + _delay = 1000; + } return this.timer = setTimeout(function() { return _this.tick(); - }, this.options.delay); + }, _delay ); } }; diff --git a/demo/assets/js/interface.js b/demo/assets/js/interface.js index bafa293..508710f 100755 --- a/demo/assets/js/interface.js +++ b/demo/assets/js/interface.js @@ -2,7 +2,7 @@ $( document ).ready( function() { $( '.tick' ).ticker( { - delay : 1000, + delay : [50, 1000], // will take random number in between 50 and 1000 ms separators : true }); -}); \ No newline at end of file +}); From 57618cb9ba94a907efd5a6576b6d963f5d54e044 Mon Sep 17 00:00:00 2001 From: Nadi Date: Sun, 26 Oct 2014 00:53:25 -0700 Subject: [PATCH 2/3] United vars. --- assets/js/tick.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/js/tick.js b/assets/js/tick.js index 66ea04c..049474b 100755 --- a/assets/js/tick.js +++ b/assets/js/tick.js @@ -141,8 +141,8 @@ }; Tick.prototype.set_timer = function() { - var _this = this; - var _delay; + var _this = this, + _delay; if (this.running) { if (this.options.delay.length == 1) { _delay = this.options.delay[0]; From b559373b32ac5a8bb850c446b1fc921d416325ce Mon Sep 17 00:00:00 2001 From: Nadi Date: Sun, 2 Nov 2014 01:33:17 -0700 Subject: [PATCH 3/3] Add verification if Array or Integer was passed as a Delay property (implemented in CoffeeScript and compiled JS). --- assets/coffee/tick.coffee | 18 ++++++++++++++---- assets/js/tick.js | 18 +++++++++--------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/assets/coffee/tick.coffee b/assets/coffee/tick.coffee index d49ee60..ebe65a3 100755 --- a/assets/coffee/tick.coffee +++ b/assets/coffee/tick.coffee @@ -141,11 +141,21 @@ class Tick set_timer: () -> + if @running + _delay = 1000 - # setInterval() can cause problems in inactive tabs (see: http://goo.gl/pToBS) - @timer = setTimeout( () => - @tick() - , @options.delay ) if @running + # if delay is Integer + _delay = @options.delay if @options.delay is parseInt(@options.delay, 10) + + # if delay is Array of at least two Integers + _delay = Math.floor( Math.random() * ( this.options.delay[1] - this.options.delay[0] + 1) + this.options.delay[0] ) if @options.delay instanceof Array and + @options.delay[0] is parseInt(@options.delay[0], 10) and + @options.delay[1] is parseInt(@options.delay[1], 10) + + # setInterval() can cause problems in inactive tabs (see: http://goo.gl/pToBS) + @timer = setTimeout( () => + @tick() + , _delay ) ### diff --git a/assets/js/tick.js b/assets/js/tick.js index 049474b..975b1df 100755 --- a/assets/js/tick.js +++ b/assets/js/tick.js @@ -141,19 +141,19 @@ }; Tick.prototype.set_timer = function() { - var _this = this, - _delay; + var _delay, + _this = this; if (this.running) { - if (this.options.delay.length == 1) { - _delay = this.options.delay[0]; - } else if (this.options.delay.length == 2) { - _delay = Math.floor( Math.random() * ( this.options.delay[1] - this.options.delay[0] + 1) + this.options.delay[0] ); - } else { - _delay = 1000; + _delay = 1000; + if (this.options.delay === parseInt(this.options.delay, 10)) { + _delay = this.options.delay; + } + if (this.options.delay instanceof Array && this.options.delay[0] === parseInt(this.options.delay[0], 10) && this.options.delay[1] === parseInt(this.options.delay[1], 10)) { + _delay = Math.floor(Math.random() * (this.options.delay[1] - this.options.delay[0] + 1) + this.options.delay[0]); } return this.timer = setTimeout(function() { return _this.tick(); - }, _delay ); + }, _delay); } };