[英]Immediately invoked function expression for setTimeout doesn't work
I'm trying to wrap a setTimeout in an immediately invoked function expression which creates the function, which then calls itself again as above, and automatically starts the loop: 我试图将setTimeout包装在立即调用的函数表达式中,该表达式创建函数,然后如上所述再次调用自身,并自动启动循环:
Here is the reference: enter link description here 参考如下: 在此处输入链接描述
For some reason it runs once and then stopped. 由于某种原因,它只运行一次然后停止。
//Spaceship move
function spaceShipMove(spaceShipToMove){
var randomY = Math.floor(Math.random());
spaceShipToMove.style.webkitTransform = 'translate(-3000px, ' + randomY + 'px)';
}
//Spaceship
var spaceShipsList = createMultipleSpaceShips(500);
for (var i = 0; i < spaceShipsList.length; i++){
var aSpaceShip = spaceShipsList[i];
app.dom.mainWrapper.appendChild(aSpaceShip);
(function t() {
setTimeout(function (spaceShipGo) {
spaceShipMove(spaceShipGo);
}, i * 300, aSpaceShip)
})();
try this 尝试这个
for (var i = 0; i < spaceShipsList.length; i++){
var aSpaceShip = spaceShipsList[i];
app.dom.mainWrapper.appendChild(aSpaceShip);
setTimeout(spaceShipMove.bind(null, aSpaceShip), i * 300)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.