javascript – 将元素的CSS更改为5秒,但如果下一个事件仍在5以内,则会保持不变

我正在用信标构建应用程序.每当一个事件被触发时,我想将背景颜色改为红色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);

相关文章

1.第一步 设置响应头 header('Access-Control-Allow...
$.inArray()方法介绍 $.inArray()函数用于在数组中搜索指定的...
jquery.serializejson.min.js的妙用 关于这个jquery.seriali...
JS 将form表单数据快速转化为object对象(json对象) jaymou...
jQuery插件之jquery.spinner数字智能增减插件 参考地址:http...