我正在用信标构建应用程序.每当一个事件被触发时,我想将背景颜色改为红色5秒,然后在5秒钟后再改回蓝色.没有复杂性这很容易我只使用$timeout和瞧.
我的问题是,如果事件在同样的5秒内再次被触发,我想将背景为红色的长度延长为5秒.
我当前的代码类型可以完成工作,但只要原来的5秒钟就会保持闪烁.
$('#reportingLoopPage').removeClass('positive-bg');
$('#reportingLoopPage').addClass('assertive-bg');
$timeout(function () {
$('#reportingLoopPage').removeClass('assertive-bg');
$('#reportingLoopPage').addClass('positive-bg');
}, 5000);
如何在5秒的持续时间内保持更改,并在重新触发事件时重置该持续时间?
我很高兴使用JS,Angular和JQuery的任何组合
解决方法:
My problem is that if the event if fired again within that same 5 seconds, I want to extend the length that the background is red to be 5 seconds from then.
您可以使用$timeout.cancel(promiseVariable)取消当前计时器并创建一个新计时器.
var promise;
//More code...
$('#reportingLoopPage').removeClass('positive-bg');
$('#reportingLoopPage').addClass('assertive-bg');
$timeout.cancel(promise);
promise = $timeout(function () {
$('#reportingLoopPage').removeClass('assertive-bg');
$('#reportingLoopPage').addClass('positive-bg');
}, 5000);