2017-07-27 75 views
1
var board = new DepartureBoard(document.getElementById('test'), { 
    rowCount: 1, 
    letterCount: 13 
}); 

window.setTimeout(function() { 
    board.setValue(['VISA SERVICES', ]); 
}, 0) 
window.setTimeout(function() { 
    board.setValue(['POE CLEARENCE', ]); 
}, 8000); 
window.setTimeout(function() { 
    board.setValue(['STAMPING', ]); 
}, 16000); 
window.setTimeout(function() { 
    board.setValue(['ATTESTATION', ]); 
}, 24000); 
window.setTimeout(function() { 
    board.setValue(['INSURANCE', ]); 
}, 32000); 
window.setTimeout(function() { 
    board.setValue(['FOREX', ]); 
}, 40000); 

https://codepen.io/Mtn_Wolf/pen/LKsvz如何循环这个函数的最后一个值

我试图像机场离开后,但我可以不能够在我的网站上显示的最后一个值后循环的值,显示其服务如何我可以循环的值之后,最后的内容显示

+0

你的意思是你想的全过程运行再一次,一遍又一遍? – skwidbreth

+0

是的,我想运行整个过程再次运行,一遍又一遍,没有任何延迟 – sureshkumar

+0

永远?它是否需要停止? – skwidbreth

回答

0

基本上你可以使用一个外部间隔,其中长度是完整的时间段,内部调用下一个动作的较小超时。

function startInterval() { 
    window.setTimeout(function() { board.setValue(['VISA SERVICES']); }, 0) 
    window.setTimeout(function() { board.setValue(['POE CLEARENCE' ]); }, 8000); 
    window.setTimeout(function() { board.setValue(['STAMPING']); }, 16000); 
    window.setTimeout(function() { board.setValue(['ATTESTATION']); }, 24000); 
    window.setTimeout(function() { board.setValue(['INSURANCE']); }, 32000); 
    window.setTimeout(function() { board.setValue(['FOREX']); }, 40000); 
} 

startInterval(); 
setInterval(startInterval, 48000); // use the length of the period 
+0

谢谢你的工作 – sureshkumar

0

有,你可以做到这一点的方法很多,这里是我的建议,基本上保持当前的代码不变 -

您的通话将所有window.setTimeout()我n函数 - 最后,再添加一个调用window.setTimeout()的函数,该函数将调用该函数,该函数将再次启动它。像...

var board = new DepartureBoard(document.getElementById('test'), { rowCount: 1, letterCount: 13 }); 

var myFunction = function(){ 
    window.setTimeout(function() { 
     board.setValue(['VISA SERVICES', ]); 
    }, 0) 
    window.setTimeout(function() { 
     board.setValue(['POE CLEARENCE', ]); 
    }, 8000); 
    window.setTimeout(function() { 
     board.setValue(['STAMPING', ]); 
    }, 16000); 
    window.setTimeout(function() { 
     board.setValue(['ATTESTATION', ]); 
    }, 24000); 
    window.setTimeout(function() { 
     board.setValue(['INSURANCE', ]); 
    }, 32000); 
    window.setTimeout(function() { 
     board.setValue(['FOREX', ]); 
    }, 40000); 
    window.setTimeout(function() { 
     myFunction(); // call it again here, and the process will start over 
    }, 50000); 
} 

myFunction(); // don't forget - now that all of the window.setTimeout calls are wrapped in a function, the process won't start unless you call the function here, setting off the loop 
+0

错误来了,所以我将“var myFunction(){”替换为“function myFunction(){”现在它的工作谢谢 – sureshkumar

+0

哦,是的 - 抱歉,我的一个错字。我打算写'var myFunction = function(){...}'...编辑... – skwidbreth

1

现代ECMAScript中使用直接超时不好的模式,不只是因为“现代方法”,但由于更好的链接和错误处理

function delay(time) { return new Promise(resolve => setTimeout(resolve, time) } 
function worker() { 
    return Promise.resolve() 
     .then(() => board.setValue(['VISA SERVICES'])) 
     .then(() => delay(1000)) 
     // and so on 
     .then(worker); 
} 

worker().catch((error) => { 
    // Error handling here 
}) 
相关问题