項目前端頁面需要實現,頁面沒人操作進入倒計時,以下為前端代碼實現。
//設置(倒計時功能)開關 var _mouseActiveListener_flag = true;
beforecount:觸發倒計時間隔(單位:毫秒)
count:倒計時總數(單位:秒)
var mouseActiveListener = function (beforecount, count, callback) {
//config
var __countdown_html = '<div id="__tt" style="position:fixed;top:110px;right:10px;z-index:1000;color:#eee;font-size:25px;"></div>';
//define
var target = null, _t = null, _tc = null;
var target_countdown = function (__count) {
if (__count >= 0) {
target.innerHTML = __count + '秒後退出';
_tc = setTimeout(function () {
target_countdown(__count);
}, 1000);
} else {
callback();
}
__count--;
}, _t_exec = function () {
return setTimeout(function () {
if (_mouseActiveListener_flag) {
target = Ne.dom.createEl(__countdown_html);
document.body.appendChild(target);
target_countdown(count);
}
}, beforecount);
}, _t_clear = function () {
clearTimeout(_t);
clearTimeout(_tc);
//target.parentElement.removeChild(target);
$(target).remove();
target = null;
};
//exec
_t = _t_exec();
document.addEventListener('click', function () {
_t_clear();
_t = _t_exec();
});
};
//後置操作,解釋:在5秒後(5000)不操作的狀態下觸發倒計時,倒計時180秒,具體看View Code裡面的函數。
mouseActiveListener(5000, 180, function () {
window.location.href = "/Home/Index";
});
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。