[英]Update progress bar in with javascript
I want to update my progress bar in html while function is running. 我想在函数运行时更新html中的进度条。 Is there any way I can do this? 有什么办法可以做到吗?
function animateProgressBar() {
var numIteration = 100;
for (var i = 1; i <= numIteration; i++) {
//do some calculations
width = (i/numIteration)*100;
htmlElement.style.width = width + '%';
}
}
The problem is that your code is blocking. 问题是您的代码正在阻塞。 Therefore the page isnt rendered, and the status bar doesnt change. 因此,该页面未呈现,并且状态栏没有更改。 You may use a setTimeout recursively: 您可以递归使用setTimeout:
function animateProgressBar(numIteration,index=1) {
width = (index/numIteration)*100;
htmlElement.style.width = width + '%';
if(index<numIteration) setTimeout(animateProgressBar,100,numIteration,index+1);
}
Start like this: 这样开始:
animateProgressBar(100); // from 1 to 100
Note that index=1 in the parameters is ES6, so just use it on modern browsers... 请注意,参数中的index = 1是ES6,因此只需在现代浏览器中使用它即可...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.