在循环中设置超时和
set Timeout and for in loop
function createDivs(){
var styleHash = {};
vertical=0;
horizontal=0;
var h;
var aDiv
var colour;
var groupStyle;
for(key in elHash){
h=elArr[0][zIndex[key]]/elHash[key];
colour = randomColor();
setLocation(h,elHash[key]);
var container = document.getElementById('container');
styleElements(container,'width',(scrnWidth-40)+'px');
styleElements(container,'height',(scrnHeight-200)+'px');
aDiv = implementDOMelement(container,'div', '');
groupStyle = function() {
styleElements(aDiv ,vposition,vertical+'px');
styleElements(aDiv ,hposition,horizontal+'px');
styleElements(aDiv ,'backgroundColor', colour);
styleElements(aDiv ,'width', elHash[key]+'px');
styleElements(aDiv ,'height', h+'px');
styleElements(aDiv ,'zIndex', zIndex[key]);
if (colour =='#ffffff'){styleElements(aDiv ,'border', 'solid');}
}
setTimeout( groupStyle ,1000);
}
}
function randomColor(){
var colorR;
var colorG;
var colorB;
colorR = randomNumber(0,255);
colorG = randomNumber(0,255);
colorB = randomNumber(0,255);
return 'rgb('+colorR+','+colorG+','+colorB+')';
}
function implementDOMelement(parentNode, elementType,innHTML, attributes ){//
var element = document.createElement(elementType);
for (key in attributes){
element.setAttribute(key,attributes[key]);
}
element.innerHTML = innHTML;
parentNode.appendChild(element);
return element;
}
function styleElements(aNode,cssProperty,cssVal){
aNode.style[cssProperty]=cssVal;
}
为什么"setTimeout"在每次迭代中只执行一次?我的目标是每秒钟弹出一个div!没有放入所有的代码,但没有setTimeOut和groupStyle(代码不在函数中(也能正常工作
10倍的帮助,BR
它在每个循环上执行,但所有10个循环都同时执行。循环非常快速地遍历列表,当它完成循环时,可能没有发生一次超时。
如果您希望每1秒执行一次aFunc
,则使用setInterval
,或者在每次迭代后将setTimeout
时间增加1000。
*心理调试*。。。我相信你的意思是:
var i = 0;
for(key in hash){
var aFunc = function() {}
setTimeout(aFunc, 1000 * i++);
}
您的函数调用一个接一个地发生,因为for
循环不需要运行时间,因此所有超时都设置为大致相同的时间。您需要增加每次迭代的超时时间,以获得每秒一次的效果。或者使用setInterval()
/clearInterval()
。
看看这个,它可能就是你想要的答案:
循环中的setTimeout不打印连续值
在您的例子中:aDiv总是指向循环的最后一个div。这就是为什么它看起来只触发一次的原因。
代码的一个简单解决方案应该是这样的:
groupStyle = function() {
styleElements(aDiv ,vposition,vertical+'px');
styleElements(aDiv ,hposition,horizontal+'px');
styleElements(aDiv ,'backgroundColor', colour);
styleElements(aDiv ,'width', elHash[key]+'px');
styleElements(aDiv ,'height', h+'px');
styleElements(aDiv ,'zIndex', zIndex[key]);
if (colour =='#ffffff'){styleElements(aDiv ,'border', 'solid');}
}
setTimeout( groupStyle ,1000);
//replaced with:
// I will assume vposition and hposition were supposed to be strings not variables
doGroupStyle(aDiv, vertical, horizontal, elHash, key, zIndex, colour);
// then create the doGroupStyle function
function doGroupStyle(aDiv, vertical, horizontal, elHash, key, zIndex, colour) {
setTimeout(function() {
styleElements(aDiv ,'vposition',vertical+'px');
styleElements(aDiv ,'hposition',horizontal+'px');
styleElements(aDiv ,'backgroundColor', colour);
styleElements(aDiv ,'width', elHash[key]+'px');
styleElements(aDiv ,'height', h+'px');
styleElements(aDiv ,'zIndex', zIndex[key]);
if (colour =='#ffffff'){styleElements(aDiv ,'border', 'solid');
}, 1000 * key);
}
}
// or an alternative approach as passcod suggested:
(function(aDiv, vertical, horizontal, colour, elHash, key, h, zIndex) {
groupStyle = function() {
styleElements(aDiv ,'vposition',vertical+'px');
styleElements(aDiv ,'hposition',horizontal+'px');
styleElements(aDiv ,'backgroundColor', colour);
styleElements(aDiv ,'width', elHash[key]+'px');
styleElements(aDiv ,'height', h+'px');
styleElements(aDiv ,'zIndex', zIndex[key]);
if (colour =='#ffffff'){styleElements(aDiv ,'border', 'solid');}
};
setTimeout(groupStyle ,1000 * key);
})(aDiv, vertical, horizontal, colour, elHash, key, h, zIndex);
还没有测试过,所以你可能需要修改一下。但这就是它背后的想法。
相关文章:
- 设置第一次执行的超时
- 当定位模式设置为“0”时,总是达到关于地理定位的超时;仅设备”;
- 为什么页面重新加载会使设置超时无效,以及如何避免
- 立即设置超时
- 如何设置$.ajax请求的超时,并在超时时间过长时进行重做
- iOS Safari Javascript设置超时问题
- 如何将嵌套设置超时转换为承诺
- 如何设置淡出或超时以提醒成功&露天矿2.2中的警告框
- 如何在量角器测试中设置$interval而不是超时
- 如何循环设置超时函数
- 关于设置超时/间隔上下文
- 清除设置超时之前的间隔将不起作用
- 在悬停功能中设置超时
- 延迟设置超时()
- 我们是否需要设置超时进行短轮询
- 如何在firefox-sdk中设置请求超时设置
- 将超时设置为layoutTemplate流星/铁
- Jquery将超时设置为selector
- 清除javascript对象中的超时设置
- MongoError:游标被杀死或超时-Meteor超时设置无效