(JavaScript实现)页面无操作倒计时退出

本文介绍了一种前端实现的倒计时功能,当用户在指定时间内未进行操作时启动倒计时,直至时间结束自动跳转页面。通过监听用户点击事件来重置倒计时,确保用户体验的同时实现了页面的自动管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 项目前端页面需要实现,页面没人操作进入倒计时,以下为前端代码实现。

//设置(倒计时功能)开关
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     };
View Code

 

//后置操作,解释:在5秒后(5000)不操作的状态下触发倒计时,倒计时180秒,具体看View Code里面的函数。
mouseActiveListener(5000, 180, function () {
        window.location.href = "/Home/Index";
    });

 

转载于:https://www.cnblogs.com/rushpm/p/javascript_01.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值