有没有办法处理使用jQuery连续计时器活动?

时间:2014-10-30 17:16:57

标签: javascript jquery

我在jQuery按钮中有以下操作:

this.$el.find(".pt_timer_button button").off("click").on("click", this.on_timer);

this.on_timer的评论如下:

on_timer: function(e) {
            var self = this;
            if ($(e.target).hasClass("pt_timer_start")) {
                current_date = this.get_current_UTCDate();
                this.project_timesheet_db.set_current_timer_activity({date: current_date});
                this.start_interval();
                this.initialize_timer();
                // this.save_interval();
                this.$el.find(".pt_timer_start,.pt_timer_stop").toggleClass("o_hidden");
                // this.resume_interval();
            }

STOP 按钮的else部分逻辑中。

现在我有变量监视计时器启动时和当前时间值之间的时差。

代码是:

start_interval: function() {
            var timer_activity = this.project_timesheet_db.get_current_timer_activity();
            var self = this;
            var newDate = new Date();
                    var refrence_date = newDate.getUTCFullYear() +"-"+ (newDate.getUTCMonth()+1) +"-"+newDate.getUTCDate()+" "+_.str.sprintf("%02d", newDate.getUTCHours())+":"+_.str.sprintf("%02d", newDate.getUTCMinutes())+":"+_.str.sprintf("%02d", newDate.getUTCSeconds());
                    var diff_in_seconds = moment.duration(moment(refrence_date).diff(moment(current_date))).asSeconds();
                    var h = parseInt(diff_in_seconds / 3600) % 24;
                    var h_hour = $(this).find("span.hours").add();
                    var h_text = parseInt(h_hour.text());
                    var m = parseInt(diff_in_seconds / 60) % 60;
                    var m_minute = $(this).find("span.minutes").add();
                    var m_text = parseInt(m_minute.text());
                    var s = parseInt(diff_in_seconds % 60);
                    var s_second = $(this).find("span.seconds");
                    var s_text = parseInt(s_second.text(s));
                    console.log("current time:", h, m, s);
                    console.log("difference of time at every moment:", hour, minute, seconds);
                });

现在解释这个场景: 在我的Html页面中,我显示的时差为 var s var m var k

现在如果我正在重新加载/刷新页面计时器自动设置为00。 如果计时器正在进行中,我希望再次按下 START 按钮,并且它应该保持继续。

提前感谢您的帮助。

如果需要任何具体细节,请写下评论。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用开始时间和状态设置Cookie?

也许,当你开始间隔时,写一个看起来像这样的cookie:

{"start":reference_date, "isRunning":1}

然后,在每个间隔上,检查当前状态。停止时,更新状态。