手机熄屏js定时器时间不准问题及解决办法

2020-09-25  本文已影响0人  __鹿__

一、应用场景

当用户需要发短信时用到了倒计时的功能,当页面有活动倒计时功能。

二、为什么会出现这个问题?

H5切换应用或手机熄屏,都是进入了后台运行,定时器在后台会停止运行。导致时间不准。百度上也有很多解释:

【 PC 上的 Firefox、Chrome 和 Safari 等浏览器,都会自动把未激活页面中的 JavaScript 定时器(setTimeout、setInterval)间隔最小值改为 1 秒以上。这是因为间隔很小的定时器一般用来做 UI 更新(例如用定时器实现的动画),让用户不可见的页面上的定时器跑慢一些,既节省资源又不会影响体验。对移动浏览器来说,内存、CPU、带宽等资源更加宝贵,移动设备上的浏览器往往会直接冻结所有未激活页面上的所有定时器。】

三、解决问题

思路:使用系统时间的时间差来实现这个功能。
1、定义一个变量传入的分钟数(TimeVal)。
2、记录页面初次进入的时间(entryTime),在定时器中每隔一秒记录一次当前时间(currentTime),currentTime - entryTime = 时间差 。
3、一般在倒计时里面都是有一个变量time;这个time--就会出现倒计时的效果,在这里 time-- 其实就是currentTime - entryTime

举例说明:我需要做一个一分钟的倒计时,8:00进入页面,entryTime = 8:00。此时倒计时60秒开始计算,八点整20秒时我手机熄屏(8:00:20),此时倒计时已经变成40秒。再打开屏幕时当前时间currentTime = 8:00:50。此时应该用8:00:50 - 8:00:00 = 50 s 。这50秒就是已经走掉的秒数,就可以得出还剩多少秒数。

四、附上代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <style>
  </style>
</head>

<body>
  <div class="boxDIv">
    <div class="OutTime" id="OutTime"></div>
  </div>
  <script>
    var TimeVal = 1; //传入分钟
    var entryTime = parseInt(new Date().getTime() / 1000);//进入页面得当前时间
    var currentTime = 0;//当前时间
    var maxtime = TimeVal * 60; // 传入分钟 * 60 = 最多走几秒
    function CountDown() {
      currentTime = parseInt(new Date().getTime() / 1000); //在定时器里面每隔一秒记录当前时间;
      var TimeDifference = (currentTime - entryTime); //时间差
      var mytime = maxtime - TimeDifference  //传入的秒数 - 已经走掉的秒数  = 当前还剩多少秒数
      if (TimeDifference <= maxtime) { //如果已经走掉的秒数 小于等于  传入的秒数
        minutes = Math.floor(mytime / 60);
        seconds = Math.floor(mytime % 60);
        console.log(minutes,seconds)
        msg = "距离结束还有" + minutes + "分" + seconds + "秒";
        document.getElementById('OutTime').innerHTML = msg;
        //if (maxtime == 5 * 60)alert("距离结束仅剩5分钟");
        // --maxtime;
      } else{
        clearInterval(timer);
        alert("时间到,结束!");
      }
    }
    timer = setInterval("CountDown()", 1000);
  </script>
</body>

</html>
<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>支付成功</title>
    <!-- <link rel="stylesheet" href="./style.css" type="text/css"> -->
    <style>
        * {
    margin: 0;
    padding: 0;
}

html {
    font: 100% "Montserrat Light", serif;
    height: 100vh;
    width: 100%;
    overflow: visible;
}

body {
    height: inherit;
    width: inherit;
}

.flex-container {
    width: 180px;
    height: 180px;
    margin: 0 auto;
   position: relative;
    background-color: #eeeeee;
    border-radius: 50%;
    color: #ffffff;
    margin-top: 100px;
}

.outbox {
    width: 85%;
    height: 85%;
    border-radius: 100%;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

.svg {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 160px;
    height: 160px;
}

.cls {
    fill: none;
    stroke: #000;
    stroke-linecap: round;
    stroke-miterlimit: 10;
    stroke-width: 4px;
    stroke-dasharray: 475;
    /*stroke-dasharray:475;代表: 虚线长475,间距475,然后重复 虚线长475,间距475 */
    stroke-dashoffset: 475;/* 偏移量 */
    /* stroke-dashoffset: 475;  用半径 * 2 * 3.1415926  得出偏移量 475 */
    transform: rotate(270deg);
    transform-origin: 50% 50%;
}

.run-anim {
    -webkit-animation-name: dash;
    -moz-animation-name: dash;
    -o-animation-name: dash;
    animation-name: dash;
    animation-duration: 0s;
    animation-play-state: paused;
    animation-fill-mode: none;
    animation-iteration-count: 1;
    animation-timing-function: linear;
}

@keyframes dash {
    0% {
        stroke: #D65E9E;
        stroke-dashoffset: 0;
    }

    100% {
        stroke: #D65E9E;
        stroke-dashoffset: 475;
    }
}


.boxDIv {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    font-size: 38px;
    font-family: Source Han Sans CN;
    font-weight: bold;
    color: #333333;
}



.fillStyle{
    fill: #fff;
    transform: rotate(0deg);
    transform-origin: 50% 50%;
}
.roundSmall{
    stroke: #e1e1e1;
    -webkit-animation-name: rotate;
    -moz-animation-name: rotate;
    -o-animation-name: rotate;
    animation-name: rotate;
    animation-duration: 0s;
    animation-play-state: paused;
    animation-fill-mode: none;
    animation-iteration-count: 1;
    animation-timing-function: linear;
}

@keyframes rotate {
    0% {
        transform: rotate(-1deg);
    }

    100% {
        transform: rotate(-360deg);
    }
}
    </style>
</head>

<body>

    <div class="flex-container">
        <div class="outbox"> </div>
        <!--  SVG AREA -->
        <svg class="svg">
            <circle id="cls" class="cls run-anim" cx="80" cy="80" r="75.5"></circle>
            <circle id="smallYuan" class="roundSmall fillStyle" cx="80" cy="4" r="4"></circle>
        </svg>
        <!--  SVG AREA END -->
        <div class="boxDIv">
            <div class="OutTime" id="OutTime"></div>
        </div>
    </div>
    <script>
        let TimeVal = 1; //传入分钟
        let entryTime = parseInt(new Date().getTime() / 1000);//进入页面的当前时间
        let currentTime = 0;//当前时间
        let maxtime = TimeVal * 60; // 分钟 * 60 = 最多走几秒
        let circle = document.getElementById('cls'); //获取大圆环
        let smallYuan = document.getElementById('smallYuan'); //获取小圆圈
        let total;//动画总时长
        let count = 0;//动画总时长
        function CountDown() {
            currentTime = parseInt(new Date().getTime() / 1000); //在定时器里面每隔一秒记录当前时间;
            let TimeDifference = (currentTime - entryTime); //时间差
            let mytime = maxtime - TimeDifference  //传入的秒数 - 已经走掉的秒数  = 当前还剩多少秒数
            if (TimeDifference <= maxtime) { //如果已经走掉的秒数 小于等于  传入的秒数
                minutes = Math.floor(mytime / 60);
                seconds = Math.floor(mytime % 60);
                console.log(minutes, seconds)
                if (seconds <= 9) {
                    seconds = '0' + seconds
                }
                if (minutes <= 9) {
                    minutes = '0' + minutes
                }
                msg = minutes + ":" + seconds;
                document.getElementById('OutTime').innerHTML = msg;
                if(count == 0){
                    circleTime();
                }
                count ++
            } else {
                count = 0;
                clearInterval(timer);
                console.log("时间到,结束!");
            }
        }
        
        function circleTime() {
            total = maxtime; 
            circle.style.animationDuration = total + "s";//动画时长
            circle.style.animationPlayState = "running";
            smallYuan.style.animationDuration = total + "s";//动画时长
            smallYuan.style.animationPlayState = "running";
            //动画播放状态 running:指定正在运行的动画
        
        }
        timer = setInterval("CountDown()", 1000);
    </script>
</body>

</html>

五、总结

这样就可以实现切换到后台或者熄屏定时器依然继续执行啦,如果对你有帮助,请点个赞再走把。

上一篇 下一篇

猜你喜欢

热点阅读