【问题标题】:Is there any way to handle on going timer activity continuous using jQuery?有没有办法使用 jQuery 来处理持续的计时器活动?
【发布时间】:2014-10-30 17:16:56
【问题描述】:

我在 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();
            }

并且在else部分给出了STOP按钮的逻辑。

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

代码是:

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 svar mvar k

现在如果我重新加载/刷新页面计时器会自动设置为 00。 如果计时器正在进行中,我想放类似的东西,而不需要再次按下 START 按钮,它应该保持继续。

提前感谢您的帮助。

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

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您是否尝试过设置一个带有开始时间和状态的 cookie?

    也许,当你开始间隔时,写一个如下所示的 cookie:

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

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

    【讨论】:

    • 是否有可能存储按钮(开始)状态?
    • 是的,您可以将其存储在 cookie 中。在上面的例子中,isRunning 属性意味着按钮的开始状态。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    相关资源
    最近更新 更多