如何在JS中关闭计时器

时间:2013-08-13 13:32:12

标签: javascript timer countdown

我在themeforest模板中有一些计时器。它的作用就像倒计时一样,但我需要把它转回来,比如“实验的持续时间”。所以,我试图找到一个指标,比如“i--”,并将其更改为“i ++”,并且计时器开始在加载模式下工作,但在重新加载页面后,所有更改都消失了。

所以,如果你给我看一个地方,在哪里更改代码,我将非常感激:)

源代码(主文件):

 (function (h) {
    h.fn.countdown = function (a, l) {
        function m(a, d) {
            return function () {
                return d.call(a)
            }
        }

        var k = "seconds minutes hours days weeks daysLeft".split(" ");
        return this.each(function () {
            function j() {
                if (0 === e.closest("html").length)clearInterval(f), d("removed"); else {
                    c--;
                    0 > c && (c = 0);
                    g = {seconds: c % 60, minutes: Math.floor(c / 60) % 60, hours: Math.floor(c / 60 / 60) % 24, days: Math.floor(c / 60 / 60 / 24), weeks: Math.floor(c / 60 / 60 / 24 / 7), daysLeft: Math.floor(c / 60 / 60 / 24) % 7};
                    for (var a = 0; a < k.length; a++) {
                        var b = k[a];
                        i[b] != g[b] && (i[b] = g[b], d(b))
                    }
                    0 == c && (clearInterval(f), d("finished"))
                }
            }

            function d(d) {
                var b = h.Event(d);
                b.date = new Date((new Date).valueOf() + c);
                b.value = i[d] || "0";
                b.toDate = a;
                b.lasting = g;
                switch (d) {
                    case "seconds":
                    case "minutes":
                    case "hours":
                        b.value = 10 > b.value ? "0" + b.value.toString() : b.value.toString();
                        break;
                    default:
                        b.value && (b.value = b.value.toString())
                }
                l.call(e, b)
            }

            if (!(a instanceof Date))if (String(a).match(/^[0-9]*$/))a = new Date(a); else if (a.match(/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})\s([0-9]{1,2})\:([0-9]{2})\:([0-9]{2})/) || a.match(/([0-9]{2,4})\/([0-9]{1,2})\/([0-9]{1,2})\s([0-9]{1,2})\:([0-9]{2})\:([0-9]{2})/))a = new Date(a); else if (a.match(/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})/) || a.match(/([0-9]{2,4})\/([0-9]{1,2})\/([0-9]{1,2})/))a = new Date(a); else throw Error("Doesn't seen to be a valid date object or string");
            var e = h(this), i = {}, g = {}, f = e.data("countdownInterval"), c = Math.floor((a.valueOf() - (new Date).valueOf()) / 1E3);
            j();
            f && clearInterval(f);
            e.data("countdownInterval", setInterval(m(e, j), 1E3));
            f = e.data("countdownInterval")
        })
    }
})(jQuery);

源代码(配置文件):

//countdown setting
$(function() {

  $('.countdown').countdown("2008/06/28", function(event) {
    var $this = $(this);
    switch(event.type) {
      case "seconds":
      case "minutes":
      case "hours":
      case "days":
        $this.find('span#'+event.type).html(event.value);
        break;
      case "finished":
        $this.hide();
        break;
    }

  });
});

1 个答案:

答案 0 :(得分:0)

为了在页面加载之间保存数据,您必须将其保存在某处。最简单的方法是使用cookies(我认为)。其他方式包括服务器端数据传输,在支持的浏览器上使用Web-SQL或IndexedDB。

那就是说,如果我正确地理解了你的问题。