javascript - javascript计时器和html进度条替换html
问题描述
大家好,在 html 页面上有一个简单的 javascript 倒数计时器和一个 html 进度条。
只有在进度条完成后,我才弄清楚如何让脚本用不同的页面替换 html 页面。
它要么几乎立即替换页面,要么根本不替换页面,或者根据我尝试过的不同事情,进度不会移动有人可以帮助我......这是当前代码。
var timeleft = 10;
var downloadTimer = setInterval(function() {
document.getElementById("progressBar").value = 10 - --timeleft;
if (timeleft <= 0)
clearInterval(downloadTimer);
}, 1000);
if (document.getElementById("progressBar").value > 10) {
window.location.replace('download.html');
}
<center><progress value="0" max="10" id="progressBar" style="margin-top: -29px; height: 10px;"></progress></center>
解决方案
你if(document.getElementById("progressBar").value > 10){...}
不在你的区间内,这意味着它只运行一次,就在区间创建之后。
此外,document.getElementById("progressBar").value
永远不会达到大于 的值10
。它恰好到达10
,但您的间隔已被告知停止。
考虑一下:
const progressEl = document.getElementById("progressBar");
let downloadTimer = setInterval(() => {
progressEl.value++;
if (progressEl.value >= progressEl.max) {
clearInterval(downloadTimer);
console.log("Would replace");
}
}, 200);
<progress value="0" max="10" id="progressBar"></progress>
推荐阅读
- ios - 从 XCTest UI 测试中的活动呼叫中打开 CallKit 屏幕
- django - 在 AWS 上自动部署我的应用程序的最佳方式是什么?
- firebase - Firebase - 如何在 where 子句中检查子集合是否不为空或为空?
- node.js - 使用命名空间的类的参数化构造函数时的编译器错误
- sql - 更新员工每工作一整年的工资
- c# - WPF 使用空字符串调用值转换器
- javascript - 如何将数据从 javascript 导入 mongoDB 数据库?
- ffmpeg - 为 sws_scale() 分配 AVFrame
- reactjs - React Native Navigation 自定义边框
- scala - Scala 初始化运行时字段中更改的正确方法:占位符/空,无或零元素?