如何更新javascript/jquery中for循环每次迭代显示的html?
对于循环的每个迭代,我将如何更改h1?此代码现在仅在完成所有操作后显示h1文本如何更新javascript/jquery中for循环每次迭代显示的html?,javascript,jquery,html,loops,iteration,Javascript,Jquery,Html,Loops,Iteration,对于循环的每个迭代,我将如何更改h1?此代码现在仅在完成所有操作后显示h1文本 for (i=0; i<array.length; i++) { $("body > h1").text("Processing #" + i); // things that take a while to do } for(i=0;i有时可以通过强制重新计算布局来强制渲染 for (i=0; i<array.length; i++) { $("body > h1").text
for (i=0; i<array.length; i++) {
$("body > h1").text("Processing #" + i);
// things that take a while to do
}
for(i=0;i有时可以通过强制重新计算布局来强制渲染
for (i=0; i<array.length; i++) {
$("body > h1").text("Processing #" + i)
.width(); // force browser to recalculate layout
// things that take a while to do
}
for(i=0;ivar数组=['1','2','3']
var i=0;
var refreshIntervalId=setInterval(函数(){
长度=数组长度;
如果(i<(数组长度+1)){
$(“h1”).text(“处理#”+i);
}否则{
clearInterval(refreshIntervalId);
}
i++
}, 1000);
使用具有一毫秒延迟的设置间隔:
var i=0, j=array.length;
var iv = setInterval(function() {
$("h1").text("Processing #" + i);
// things that take a while to do
if (++i>=j) clearInterval(iv);
}, 1);
我花了一点时间编写了一个jquery函数,似乎可以解决这个问题。基本上,它是一个进程处理程序,您可以添加任意数量的进程,然后调用运行,以异步方式顺序调用这些进程
$.fn.LongProcess = function () {
var _this = this;
this.notifications = [];
this.actions = [];
this.add = function (_notification, _action) {
this.notifications.push(_notification);
this.actions.push(_action);
};
this.run = function () {
if (!_this.actions && !_this.notifications) {
return "Empty";
}
//******************************************************************
//This section makes the actions lag one step behind the notifications.
var notification = null;
if (_this.notifications.length > 0) notification = _this.notifications.shift();
var action = null;
if ((_this.actions.length >= _this.notifications.length + 2) || (_this.actions.length > 0 && _this.notifications.length == 0))
action = _this.actions.shift();
//****************************************************************
if (!action && !notification) {
return "Completed";
}
if (action) action();
if (notification) notification();
setTimeout(_this.run, 1000);
//setTimeout(_this.run,1); //set to 1 after you've entered your actual long running process. The 1000 is there to just show the delay.
}
return this;
};
如何与
一起使用:
如果进程很长,可以使用此脚本显示特定时间间隔内的每个通知
这是密码
html
<div id="ccNotificationBox"></div>
javascript
var coccoNotification=(function(){
var
nA=[],
nB,
rdy=true;
function nP(a){
nA.push(a);
!rdy||(nR(),rdy=false);
}
function nR(){
nB.innerHTML=nA[0];console.log(nA[0]);
nB.offsetWidth=nB.offsetWidth;//reflow ios
nB.classList.add('active');
}
function nC(){
nB.classList.remove('active');
nB.innerHTML='';
nA.shift();
nA.length>0?nR():(rdy=true);
}
function init(){
nB=document.getElementById('ccNotificationBox');
nB.addEventListener('webkitAnimationEnd',nC,false);
window.removeEventListener('load',init,false);
}
window.addEventListener('load',init,false);
return nP
})();
用法
Coconotification(“通知1”);
示例
信息
上面的例子非常适合外部js,因为您只使用了一个全局变量,即函数名……在我的例子中,coconotification
这里有一种不同的方法,但它的作用是相同的
浏览器将不会渲染,直到您将控制权交还给它。这意味着它将不会渲染,直到您退出for循环。但通常会有一些技巧迫使它重新渲染。尽管我确实建议您在每次迭代中重写循环,以使其屈服于浏览器,然后继续…因此窗口不会锁定在美国呃,“虽然我真的建议你重写你的循环,让它在每次迭代中都能进入浏览器,然后再继续”我该怎么做?谢谢。我在回答中添加了一个例子。
$(function () {
var process = $().LongProcess();
//process.add(notification function, action function);
process.add(function () {
$(".processStatus").text("process1");
}, function () {
//..long process stuff
alert("long process 1");
});
process.add(function () {
$(".processStatus").text("process2");
}, function () {
//..long process stuff
alert("long process 2");
});
process.add(function () {
$(".processStatus").text("process3");
}, function () {
//..long process stuff
alert("long process 3");
});
process.run();
});
<div id="ccNotificationBox"></div>
#ccNotificationBox{
-webkit-animation-name:;
-webkit-animation-duration:2s;/*Notification duration*/
box-sizing:border-box;
border-radius:16px;
padding:16px;
background-color:rgba(0,0,0,0.7);
top:-100%;
right:16px;
position:fixed;
color:#fff;
}
#ccNotificationBox.active{
-webkit-animation-name:note;
top:16px;
}
@-webkit-keyframes note{
0% {opacity:0;}
20% {opacity:1;}
80% {opacity:1;}
100% {opacity:0;}
}
var coccoNotification=(function(){
var
nA=[],
nB,
rdy=true;
function nP(a){
nA.push(a);
!rdy||(nR(),rdy=false);
}
function nR(){
nB.innerHTML=nA[0];console.log(nA[0]);
nB.offsetWidth=nB.offsetWidth;//reflow ios
nB.classList.add('active');
}
function nC(){
nB.classList.remove('active');
nB.innerHTML='';
nA.shift();
nA.length>0?nR():(rdy=true);
}
function init(){
nB=document.getElementById('ccNotificationBox');
nB.addEventListener('webkitAnimationEnd',nC,false);
window.removeEventListener('load',init,false);
}
window.addEventListener('load',init,false);
return nP
})();