From 6edb57796d028e004d0a7bb9b28f3de35f3eb353 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sat, 4 Jun 2011 00:55:02 +0200 Subject: [PATCH] Do the math, once. --- 4chan_x.user.js | 21 ++++++++++----------- script.coffee | 18 ++++++++---------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index a20d5e4fb..dd5422807 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1021,26 +1021,25 @@ }; cooldown = { init: function() { - var duration; - if (0 < (duration = Math.ceil(($.getValue(g.BOARD + '/cooldown', 0) - Date.now()) / 1000))) { - cooldown.start(duration); + if (Date.now() < $.getValue(g.BOARD + '/cooldown', 0)) { + cooldown.start(); } return $.bind(window, 'storage', function(e) { if (e.key === ("AEOS.4chan_x." + g.BOARD + "/cooldown")) { - return cooldown.start(Math.ceil(($.getValue(g.BOARD + '/cooldown', 0) - Date.now()) / 1000)); + return cooldown.start(); } }); }, - start: function(duration) { + start: function() { var submit, _i, _len, _ref; + cooldown.duration = Math.ceil(($.getValue(g.BOARD + '/cooldown', 0) - Date.now()) / 1000); _ref = $$('#com_submit'); for (_i = 0, _len = _ref.length; _i < _len; _i++) { submit = _ref[_i]; - submit.value = duration; + submit.value = cooldown.duration; submit.disabled = true; } - cooldown.interval = window.setInterval(cooldown.cb, 1000); - return cooldown.duration = duration; + return cooldown.interval = window.setInterval(cooldown.cb, 1000); }, cb: function() { var submit, _i, _len, _ref; @@ -1107,9 +1106,9 @@ } } if ($.config('Cooldown')) { - duration = qr.sage ? 60 : 30; - $.setValue(g.BOARD + '/cooldown', Date.now() + duration * 1000); - cooldown.start(duration); + duration = qr.sage ? 60000 : 30000; + $.setValue(g.BOARD + '/cooldown', Date.now() + duration); + cooldown.start(); } } Recaptcha.reload(); diff --git a/script.coffee b/script.coffee index 532361ff2..e49ef1f2c 100644 --- a/script.coffee +++ b/script.coffee @@ -780,17 +780,15 @@ options = cooldown = init: -> - if 0 < duration = Math.ceil ($.getValue(g.BOARD+'/cooldown', 0) - Date.now()) / 1000 - cooldown.start duration - $.bind window, 'storage', (e) -> if e.key is "AEOS.4chan_x.#{g.BOARD}/cooldown" - cooldown.start Math.ceil ($.getValue(g.BOARD+'/cooldown', 0) - Date.now()) / 1000 + cooldown.start() if Date.now() < $.getValue g.BOARD+'/cooldown', 0 + $.bind window, 'storage', (e) -> cooldown.start() if e.key is "AEOS.4chan_x.#{g.BOARD}/cooldown" - start: (duration) -> + start: -> + cooldown.duration = Math.ceil ($.getValue(g.BOARD+'/cooldown', 0) - Date.now()) / 1000 for submit in $$ '#com_submit' - submit.value = duration + submit.value = cooldown.duration submit.disabled = true cooldown.interval = window.setInterval cooldown.cb, 1000 - cooldown.duration = duration cb: -> cooldown.duration-- @@ -842,9 +840,9 @@ qr = else $.rm dialog if $.config 'Cooldown' - duration = if qr.sage then 60 else 30 - $.setValue g.BOARD+'/cooldown', Date.now() + duration * 1000 - cooldown.start duration + duration = if qr.sage then 60000 else 30000 + $.setValue g.BOARD+'/cooldown', Date.now() + duration + cooldown.start() Recaptcha.reload() $('iframe[name=iframe]').src = 'about:blank'