From 7bb457bb42eac2d63adfbb6f7f2faa6b60f0fdc9 Mon Sep 17 00:00:00 2001 From: KuMoNKo Date: Wed, 12 May 2021 17:25:49 +0200 Subject: [PATCH 1/4] Fixed delay in background While in background, the setTimeOut function is not reliable, and adds a delay over time. --- lib/jquery-backward-timer.src.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/jquery-backward-timer.src.js b/lib/jquery-backward-timer.src.js index 7bac6ca..961104b 100644 --- a/lib/jquery-backward-timer.src.js +++ b/lib/jquery-backward-timer.src.js @@ -32,6 +32,7 @@ , plugin = this plugin.seconds_left = 0 + plugin.final_hour = Math.floor(Date.now()) + 5 plugin.target = $(element) plugin.timeout = undefined plugin.settings = {} @@ -71,6 +72,7 @@ } } , reset: function() { + plugin.final_hour = parseInt(plugin.settings.seconds) + Math.floor(Date.now()) plugin.seconds_left = plugin.settings.seconds plugin.methods._render_seconds() } @@ -94,7 +96,7 @@ var step = plugin.settings.step } - plugin.seconds_left -= step + plugin.seconds_left = (plugin.final_hour - Math.floor(Date.now())) var interval = step * 1000 plugin.timeout = setTimeout(plugin.methods._on_tick, From 78cc277f18251099ecc52d2c4b038788fb02db30 Mon Sep 17 00:00:00 2001 From: KuMoNKo Date: Wed, 12 May 2021 17:33:09 +0200 Subject: [PATCH 2/4] Update minified js --- lib/jquery-backward-timer.min.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/jquery-backward-timer.min.js b/lib/jquery-backward-timer.min.js index 3d5e461..2d4202b 100644 --- a/lib/jquery-backward-timer.min.js +++ b/lib/jquery-backward-timer.min.js @@ -15,5 +15,4 @@ * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details. * */ -!function(a){a.backward_timer=function(b){var c={seconds:5,step:1,stop_at_zero:!0,format:"h%:m%:s%",value_setter:void 0,on_start:function(){},on_cancel:function(){},on_exhausted:function(){},on_tick:function(){}},d=this;d.seconds_left=0,d.target=a(b),d.timeout=void 0,d.settings={},d.methods={init:function(b){d.settings=a.extend({},c,b),void 0==d.settings.value_setter&&(d.settings.value_setter=d.target.is("input")?"val":"text"),d.methods.reset()},start:function(){if(void 0==d.timeout&&!d.methods._is_exhausted()){d.settings.on_start(d);var a=d.seconds_left==d.settings.seconds?0:1e3*d.settings.step;setTimeout(d.methods._on_tick,a,a)}},cancel:function(){void 0!=d.timeout&&(clearTimeout(d.timeout),d.timeout=void 0,d.settings.on_cancel(d))},reset:function(){d.seconds_left=d.settings.seconds,d.methods._render_seconds()},_on_tick:function(a){if(0!=a&&d.settings.on_tick(d),d.methods._render_seconds(),d.methods._is_exhausted())d.timeout=void 0,d.settings.on_exhausted(d);else{if(d.seconds_lefta?"0"+a:""+a},_is_exhausted:function(){return d.seconds_left<=0&&d.settings.stop_at_zero}}},a.fn.backward_timer=function(b){var c=arguments;return this.each(function(){var d=a(this).data("backward_timer");return void 0==d&&(d=new a.backward_timer(this),a(this).data("backward_timer",d)),d.methods[b]?d.methods[b].apply(this,Array.prototype.slice.call(c,1)):"object"!=typeof b&&b?(a.error("Method "+b+" does not exist on jQuery.backward_timer"),void 0):d.methods.init.apply(this,c)})}}(jQuery); - +!function(t){t.backward_timer=function(e){var s={seconds:5,step:1,stop_at_zero:!0,format:"h%:m%:s%",value_setter:void 0,on_start:function(t){},on_cancel:function(t){},on_exhausted:function(t){},on_tick:function(t){}},o=this;o.seconds_left=0,o.final_hour=Math.floor(Date.now())+5,o.target=t(e),o.timeout=void 0,o.settings={},o.methods={init:function(e){o.settings=t.extend({},s,e),null==o.settings.value_setter&&(o.target.is("input")?o.settings.value_setter="val":o.settings.value_setter="text"),o.methods.reset()},start:function(){if(null==o.timeout&&!o.methods._is_exhausted()){o.settings.on_start(o);var t=o.seconds_left==o.settings.seconds?0:1e3*o.settings.step;setTimeout(o.methods._on_tick,t,t)}},cancel:function(){null!=o.timeout&&(clearTimeout(o.timeout),o.timeout=void 0,o.settings.on_cancel(o))},reset:function(){o.final_hour=parseInt(o.settings.seconds)+Math.floor(Date.now()),o.seconds_left=o.settings.seconds,o.methods._render_seconds()},_on_tick:function(t){if(0!=t&&o.settings.on_tick(o),o.methods._render_seconds(),o.methods._is_exhausted())o.timeout=void 0,o.settings.on_exhausted(o);else{if(o.seconds_left Date: Wed, 12 May 2021 17:39:09 +0200 Subject: [PATCH 3/4] Fixed Date.now() return in milliseconds to seconds --- lib/jquery-backward-timer.src.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jquery-backward-timer.src.js b/lib/jquery-backward-timer.src.js index 961104b..d7bbacf 100644 --- a/lib/jquery-backward-timer.src.js +++ b/lib/jquery-backward-timer.src.js @@ -72,7 +72,7 @@ } } , reset: function() { - plugin.final_hour = parseInt(plugin.settings.seconds) + Math.floor(Date.now()) + plugin.final_hour = parseInt(plugin.settings.seconds) + Math.floor(Date.now()/1000) plugin.seconds_left = plugin.settings.seconds plugin.methods._render_seconds() } @@ -96,7 +96,7 @@ var step = plugin.settings.step } - plugin.seconds_left = (plugin.final_hour - Math.floor(Date.now())) + plugin.seconds_left = (plugin.final_hour - Math.floor(Date.now()/1000)) var interval = step * 1000 plugin.timeout = setTimeout(plugin.methods._on_tick, From bf9a10c03698e545f5b548bdcf654b29d54d44e4 Mon Sep 17 00:00:00 2001 From: KuMoNKo Date: Wed, 12 May 2021 17:41:07 +0200 Subject: [PATCH 4/4] Update jquery-backward-timer.min.js --- lib/jquery-backward-timer.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jquery-backward-timer.min.js b/lib/jquery-backward-timer.min.js index 2d4202b..d8f8921 100644 --- a/lib/jquery-backward-timer.min.js +++ b/lib/jquery-backward-timer.min.js @@ -15,4 +15,4 @@ * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details. * */ -!function(t){t.backward_timer=function(e){var s={seconds:5,step:1,stop_at_zero:!0,format:"h%:m%:s%",value_setter:void 0,on_start:function(t){},on_cancel:function(t){},on_exhausted:function(t){},on_tick:function(t){}},o=this;o.seconds_left=0,o.final_hour=Math.floor(Date.now())+5,o.target=t(e),o.timeout=void 0,o.settings={},o.methods={init:function(e){o.settings=t.extend({},s,e),null==o.settings.value_setter&&(o.target.is("input")?o.settings.value_setter="val":o.settings.value_setter="text"),o.methods.reset()},start:function(){if(null==o.timeout&&!o.methods._is_exhausted()){o.settings.on_start(o);var t=o.seconds_left==o.settings.seconds?0:1e3*o.settings.step;setTimeout(o.methods._on_tick,t,t)}},cancel:function(){null!=o.timeout&&(clearTimeout(o.timeout),o.timeout=void 0,o.settings.on_cancel(o))},reset:function(){o.final_hour=parseInt(o.settings.seconds)+Math.floor(Date.now()),o.seconds_left=o.settings.seconds,o.methods._render_seconds()},_on_tick:function(t){if(0!=t&&o.settings.on_tick(o),o.methods._render_seconds(),o.methods._is_exhausted())o.timeout=void 0,o.settings.on_exhausted(o);else{if(o.seconds_left