【问题标题】:Restart timer after clearTimeout(timer)clearTimeout(timer) 后重启定时器
【发布时间】:2017-09-17 06:01:02
【问题描述】:

clearTimeout(timer)后如何重新启动另一个函数?

refreshTable() 函数在 7 秒不活动(无 mousemove 事件)后停止。客户端在不活动后移动鼠标是否可以重新刷新?

如果可能的话,我想保留最上面的函数 refreshTable()。

Codepen demo

//====DONT EDIT THIS FUNCTION IF POSSIBLE====
function refreshTable() {
    window.clearTimeout(timer);
    timer = window.setTimeout(refreshTable, 5000);
    $("body").append("<p>refreshTable every 5 seconds.</p>");
}
//========

var timer = window.setTimeout(refreshTable, 0);




// If theres no activity for 7 seconds do something
var activityTimeout = setTimeout(clientInActive, 7000);

function clientResetActive(){
    $("body").attr('class', 'active');   
    clearTimeout(activityTimeout);
    activityTimeout = setTimeout(clientInActive, 5000);
    //RESTART TIMER WHILE resetActive
    //????????
}

// No activity do something.
function clientInActive(){
    $("body").attr('class', 'clientInactive');
    $("body").append("<p>clientInActive</p>");
    //STOP TIMER WHILE clientInActive
    clearTimeout(timer);
}

// Check for mousemove, could add other events here such as checking for key presses ect.
$(document).bind('mousemove', function(){clientResetActive()});

目标如下图所示。

【问题讨论】:

  • 能否请您更清楚地了解您想要实现的目标,想要帮助但不了解您需要什么!
  • 顶级函数(refreshTable()) 我想在客户端处于非活动状态时停止/挂起。当客户端(返回)移动鼠标时,refreshTable() 重新启动或继续。谢谢
  • “不活跃”是一个词,所以我不确定骆驼套管是否有帮助。它使阅读 IMO 变得有点困难,因为它让我思考“积极地做什么?”。 clientInactive 是一个更容易理解的变量名。
  • 将 var 名称更改为 clientInActive 感谢名称提示。

标签: javascript jquery refresh restart cleartimeout


【解决方案1】:

我建议您有一个负责启动超时的函数和一个负责停止它的函数。试试这个:

//====DONT EDIT THIS TOP FUNCTION IF POSSIBLE====
function refreshTable() {
    stopRefreshTable();
    window.refreshTableTimer = window.setTimeout(refreshTable, 5000);
    $("body").append("<p>refreshTable every 5 seconds.</p>");
}
//====END====

function startRefreshTable() {
    if(!window.refreshTableTimer) {
        window.refreshTableTimer = window.setTimeout(refreshTable, 0);
    }
}

function stopRefreshTable() {
    if(window.refreshTableTimer) {
        self.clearTimeout(window.refreshTableTimer);
    }
    window.refreshTableTimer = null;
}

function resetActive(){
    $("body").attr('class', 'active');   
    clearTimeout(activityTimeout);
    activityTimeout = setTimeout(inActive, 5000);
    //RESTART TIMER WHILE resetActive
    startRefreshTable()
}

// No activity do something.
function inActive(){
    $("body").attr('class', 'inactive');
    $("body").append("<p>inActive</p>");
    //STOP TIMER WHILE inActive
    stopRefreshTable();
}

// If theres no activity for 7 seconds do something
var activityTimeout = setTimeout(inActive, 7000);
// Check for mousemove, could add other events here such as checking for key presses ect.
$(document).bind('mousemove', function(){resetActive()});
startRefreshTable();
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

【讨论】:

  • 不错。谢谢楼主!
  • 一个小改动是让activityTimeout = setTimeout(inActive, 7000);而不是 5000。
猜你喜欢
  • 1970-01-01
  • 2020-09-12
  • 2014-05-29
  • 1970-01-01
  • 2017-02-27
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多