jquery 在输入更改时扩展 setTimeout 函数

标签 jquery settimeout

我正在尝试为setTimeout()添加额外的时间这是通过单独的 .on('click') 启动的功能。

请按照我的代码描述来了解发生了什么

// my control opacity jQuery object
var $controlOpacity = $('#control-opacity');

// when the child button is clicked it reveals my input range slider
$controlOpacity.on('click','BUTTON', function() {

    // this toggles a open class to reveal my input range slider
    $controlOpacity.toggleClass('open');

    // begin the 5 second set timeout function 
    setTimeout(function(){

        // removes the open class to hide my input range slider
        $controlOpacity.removeClass('open');

    }, 5000);

});

// my input range slider inside my control opacity div
$('INPUT', $controlOpacity).change(function() {

    // how can I add another 5 seconds to setTimeout function via this function?

});

因此,当我的输入更改功能触发时,我希望能够向 setTimeout 再添加 5 秒当前正在通过 .on('click') 运行的函数功能

我确信有一个明显的方法可以做到这一点,但是实现此目的最干净的 jQuery 解决方案是什么?

谢谢

最佳答案

您无法更改正在进行的 setTimeout 的超时- 相反,您必须明确 clearTimeout在现有超时情况下,然后调用 setTimeout再次使用新的持续时间。为此,您需要一个持久变量来记录下一次超时(如果有)何时由触发引起,否则为 0 - 当第二个监听器触发时,检查该变量与 Date.now() 之间的差异。找出新的超时需要持续多长时间:

const removeFn = () => {
  $controlOpacity.removeClass('open');
  triggerAt = 0;
};
let timeout;
let triggerAt = 0;

$controlOpacity.on('click','BUTTON', function() {
  $controlOpacity.toggleClass('open');
  timeout = setTimeout(removeFn, 5000);
  // Set triggerAt so we can determine how long the timeout has left later
  triggerAt = Date.now() + 5000;
});

$('INPUT', $controlOpacity).change(function() {
  // Clear the existing timeout, if it exists
  clearTimeout(timeout);
  // If no timeout is running, return early
  if (!triggerAt) return;
  // Figure out how much time was remaining on the existing timeout
  const remaining = triggerAt - Date.now();
  // Set the new timeout for 5000 ms plus however long the old timeout had left
  timeout = setTimeout(removeFn, remaining + 5000);
  triggerAt += 5000;
});

ES5 版本:

var removeFn = function() {
  $controlOpacity.removeClass('open');
  triggerAt = 0;
};
var timeout;
var triggerAt = 0;

$controlOpacity.on('click','BUTTON', function() {
  $controlOpacity.toggleClass('open');
  timeout = setTimeout(removeFn, 5000);
  // Set triggerAt so we can determine how long the timeout has left later
  triggerAt = Date.now() + 5000;
});

$('INPUT', $controlOpacity).change(function() {
  // Clear the existing timeout, if it exists
  clearTimeout(timeout);
  // If no timeout is running, return early
  if (!triggerAt) return;
  // Figure out how much time was remaining on the existing timeout
  var remaining = triggerAt - Date.now();
  // Set the new timeout for 5000 ms plus however long the old timeout had left
  timeout = setTimeout(removeFn, remaining + 5000);
  triggerAt += 5000;
});

正如您的问题所问,这现有超时延长了 5000 毫秒 - 另一种选择,它只是清除现有超时并在未来,将需要更少的代码,因为您只需跟踪超时是否持续,而不是剩余时间:

const removeFn = () => {
  $controlOpacity.removeClass('open');
  running = false;
};
let timeout;
let running = false;

$controlOpacity.on('click','BUTTON', function() {
  $controlOpacity.toggleClass('open');
  timeout = setTimeout(removeFn, 5000);
  running = true;
});
$('INPUT', $controlOpacity).change(function() {
  clearTimeout(timeout);
  if (!running) return;
  timeout = setTimeout(removeFn, 5000);
});

ES5 版本:

var removeFn = function() {
  $controlOpacity.removeClass('open');
  running = false;
};
var timeout;
var running = false;

$controlOpacity.on('click','BUTTON', function() {
  $controlOpacity.toggleClass('open');
  timeout = setTimeout(removeFn, 5000);
  running = true;
});
$('INPUT', $controlOpacity).change(function() {
  clearTimeout(timeout);
  if (!running) return;
  timeout = setTimeout(removeFn, 5000);
});

如果无法触发change事件时open类不存在,那么它会变得更加容易,因为不需要检查超时是否正在运行:

const removeFn = () => {
  $controlOpacity.removeClass('open');
};
let timeout;
$controlOpacity.on('click','BUTTON', function() {
  $controlOpacity.toggleClass('open');
  timeout = setTimeout(removeFn, 5000);
});
$('INPUT', $controlOpacity).change(function() {
  clearTimeout(timeout);
  timeout = setTimeout(removeFn, 5000);
});

ES5 版本:

var removeFn = function() {
  $controlOpacity.removeClass('open');
};
var timeout;
$controlOpacity.on('click','BUTTON', function() {
  $controlOpacity.toggleClass('open');
  timeout = setTimeout(removeFn, 5000);
});
$('INPUT', $controlOpacity).change(function() {
  clearTimeout(timeout);
  timeout = setTimeout(removeFn, 5000);
});

关于jquery 在输入更改时扩展 setTimeout 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53017949/

相关文章:

javascript - 检测用户何时在 div 外部单击

jquery - jQuery Animate 和 "easeOutBounce"的问题

angular - 不能在 Angular 2 的 setTimeout 方法中使用 router.navigate

javascript - 如何禁用所有 setTimeout 事件?

javascript - 如何告诉 .hover() 等待?

javascript - Firefox setTimeout(func, ms) 发送默认参数给回调

javascript - AngularJS - 无论返回的 http 状态如何,如何捕获从 GET 服务方法返回的 JSON

javascript - 在 div 中使用箭头键

javascript - 将每个 div 附加到自己的父级

Javascript,简单的回调示例未按预期工作