1***@163.com
1***@163.com
  • 发布:2018-12-05 15:04
  • 更新:2022-02-11 20:52
  • 阅读:7571

我设置的setInterval为20毫秒,小程序里正常,在app里的被强制变更1000毫秒,不知道是BUG还是什么问题

分类:uni-app

我写了一个定时器,每20毫秒赋一次值,显示进度条的加载效果,达到最大值时停止,写好在小程序里是正常的,在app中运行时,变成了每1000毫秒才执行一次,而不是每20毫秒执行一次。不知道是bug还是什么方面的问题。
代码我是写在onShow里的,递增electricity的值是为了控制进度条的加载动画
let electricity = that.electricity;
that.electricity = 0;
let int = setInterval(function() {
if (electricity == 0) {
return;
}
that.electricity++;
console.log(that.electricity);
if (electricity == that.electricity) {
clearInterval(int);
}
}, 20); //这里设置的20毫秒,在app里是每1000毫秒才执行一次

2018-12-05 15:04 负责人:无 分享
已邀请:
Trust

Trust - 少说废话

测试代码

<template>  
    <view>  
        Hello  
    </view>  
</template>  

<script>  
    export default {  
        data() {  
            return {  
                timer: null,  
                count: 0,  
                total: 0  
            };  
        },  
        onReady() {  
            console.log(Date.now())  
            this.timer = setInterval(() => {  
                this.count += 1;  
                this.total += 20;  
                if(this.count === 5) {  
                    clearInterval(this.timer)  
                    console.log('total:' + this.total)  
                    console.log(Date.now())  
                }  
            }, 20);  
        }  
    }  
</script>  

<style>  
</style>

测试结果

可能测试的场景有所差异,建议上传一个可以重现此问题的精简项目。

1***@163.com

1***@163.com (作者) - 95前端IT男

写法和我的一样的,没什么问题。你看我的打印时间是1000毫秒才打印一次。
//this.electricity = 50; this.iniNum=0;
this.iniNum = this.electricity;
this.electricity = 0;
var ints = setInterval(() => {
if (this.iniNum === 0) {
return;
};
this.electricity += 1;
console.log('this.electricity:' + this.electricity)
console.log(Date.now())
if (this.iniNum === this.electricity) {
clearInterval(ints);
}
}, 17);
小程序正常,app不行。测试的手机是华为p10

h***@21cn.com

h***@21cn.com

有其他可以代替的方法吗,
我在真机上测试, setInterval真是卡

该问题目前已经被锁定, 无法添加新回复