项目前端页面需要实现,页面没人操作进入倒计时,以下为前端代码实现。
//设置(倒计时功能)开关 var _mouseActiveListener_flag = true;
beforecount:触发倒计时间隔(单位:毫秒)
count:倒计时总数(单位:秒)


1 var mouseActiveListener = function (beforecount, count, callback) { 2 //config 3 var __countdown_html = '<div id="__tt" style="position:fixed;top:110px;right:10px;z-index:1000;color:#eee;font-size:25px;"></div>'; 4 //define 5 var target = null, _t = null, _tc = null; 6 var target_countdown = function (__count) { 7 if (__count >= 0) { 8 target.innerHTML = __count + '秒后退出'; 9 _tc = setTimeout(function () { 10 target_countdown(__count); 11 }, 1000); 12 } else { 13 callback(); 14 } 15 __count--; 16 }, _t_exec = function () { 17 return setTimeout(function () { 18 if (_mouseActiveListener_flag) { 19 target = Ne.dom.createEl(__countdown_html); 20 document.body.appendChild(target); 21 target_countdown(count); 22 } 23 }, beforecount); 24 }, _t_clear = function () { 25 clearTimeout(_t); 26 clearTimeout(_tc); 27 //target.parentElement.removeChild(target); 28 $(target).remove(); 29 target = null; 30 }; 31 //exec 32 _t = _t_exec(); 33 document.addEventListener('click', function () { 34 _t_clear(); 35 _t = _t_exec(); 36 }); 37 };
//后置操作,解释:在5秒后(5000)不操作的状态下触发倒计时,倒计时180秒,具体看View Code里面的函数。 mouseActiveListener(5000, 180, function () { window.location.href = "/Home/Index"; });