如何在每一次“暂停”的迭代过程中添加一个暂停;对于循环“;使用Javascript
How to add a pause during every iteration of a "for loop" using Javascript?
场景:我有一组单词及其相应的含义,我必须以不同的DIV间隔显示。以下是我写的代码:
<body>
<div id="worsd">
</div>
<div id="meaning">
</div>
<script>
var wordss = [["word1","meaning1"],["word2","meaning2"],["word3","meaning3"]];
for(var i =0;i<wordss.length;i++)
{
var hellooo = wordss[i][0];
var hellooo1 = wordss[i][1];
document.getElementById('worsd').innerHTML = hellooo;
document.getElementById('meaning').innerHTML = hellooo1;
}
</script>
</body>
请通过提供宝贵的指导来帮助我实现这一目标。
非常感谢!
您不能延迟,但您可以设置一个定期更新文本的计时器。
我在命名变量时保留了这些变量,但我将这些变量封装在一个立即调用的函数表达式中,以保持全局范围的干净。顺便说一下,这没有必要。
<body>
<div id="worsd">
</div>
<div id="meaning">
</div>
<script>
(function() // Wrap in immediately invoked function.
{
var wordss = [["word1","meaning1"],["word2","meaning2"],["word3","meaning3"]];
var i = 0;
// Function that just shows the next word every time it is called.
function nextWord() {
var hellooo = wordss[i][0];
var hellooo1 = wordss[i][1];
document.getElementById('worsd').innerHTML = hellooo;
document.getElementById('meaning').innerHTML = hellooo1;
if (++i >= wordss.length)
i = 0; // Wrap when the last element is passed.
};
// Set a timer to call the function every 2 seconds.
setInterval(nextWord, 2000);
// Show the first word right away.
nextWord();
})();
</script>
</body>
var arrOfWords = [] // words here
function showWords(index) {
// do your words display stuff here
}
var counter = 0;
var timeOut = setInterval(function(){
counter++;
if(counter < arrOfWords.length)
showWords(counter)
else
clearInterval(timeOut)
}, 2000) // ms of loop
showWords(counter) // be sure to handle the first one
您必须使用递归函数来执行您想要的操作。例如:
// timer function that loops through an array in a given interval
function timer(list, callback, time /*, onFinish, index*/ ) {
var onFinish = arguments.length > 3 ? arguments[3] : void 0,
index = arguments.length > 4 ? arguments[4] : 0;
if (index < list.length) {
callback.call(this, index, list[index]);
list.__timed = setTimeout(function() {
timer(list, callback, time, onFinish, ++index);
}, time);
} else if (onFinish) {
onFinish.call(this);
}
return {
cancel: function() {
if (list.__timed !== void 0) {
clearTimeout(list.__timed);
delete list.__timed;
}
}
};
}
document.addEventListener('DOMContentLoaded', function() {
var wordss = [
["word1", "meaning1"],
["word2", "meaning2"],
["word3", "meaning3"]
];
timer(wordss, function(index, item) {
var hellooo = item[0];
var hellooo1 = item[1];
document.getElementById('worsd').innerHTML = hellooo;
document.getElementById('meaning').innerHTML = hellooo1;
}, 3000);
});
<body>
<div id="worsd">
</div>
<div id="meaning">
</div>
</body>
上面的timer
函数可以为任何数组调用,为您想要的内容以及您想要在迭代之间延迟的时间传递回调函数。
相关文章:
- 如何将此转盘调整为仅当用户将鼠标悬停在其中一个框上时暂停
- 创建一个只有播放和暂停的Javascript音频播放列表
- Jquery 函数创建一个异常的暂停
- 我正在从API提取一个包含视频的Object标签.如何在javascript中暂停视频
- jquery对话框暂停一个类似alert()的脚本
- Javascript 暂停 - 禁用一个函数,直到另一个函数完成
- HTML5 视频在开始播放另一个视频时暂停使用 Javascript
- 另一个jQuery滑块在悬停时暂停
- Javascript幻灯片与播放暂停和下一个上一个按钮
- 使用一个功能播放/暂停多个视频
- Froogaloop的“完成”事件也会触发一个“暂停”事件
- 暂停另一个函数的 JS 函数
- 如何创建一个带有导航箭头和播放/暂停的图像转盘,点击后会发生变化
- 在另一个功能完成时暂停功能
- 如何暂停一个javascript动画事件
- 在javascript中是否有任何方法可以暂停一个函数在中间等待输入
- 我如何暂停一个RxJS缓冲的可观察对象基于在缓冲区中的值,因为他们被评估
- 暂停一个函数“onclick”
- 暂停一个动画2秒,然后在jQuery中自动恢复它(不在鼠标悬停或鼠标移出中)
- 暂停一个函数,等待另一个函数执行