简体   繁体   English

使用Vanilla JS自动运行多次循环

[英]Running a loop multiple times automatically with vanilla JS

I have succeeded in cobbling together pieces of code that achieve my goal. 我已经成功地将实现我的目标的代码片段整理在一起。 However, I would like some advice from more advanced vanilla JS programmers on how I can go about reaching my goal in a better way. 但是,我想从更高级的JS初学者那里获得一些建议,以更好地实现自己的目标。

To start, I want to introduce my problem. 首先,我想介绍一下我的问题。 I have a piece of text on my website where a portion is designed to change every so often. 我的网站上有一段文字,其中一部分旨在经常更改。 For this, I am running through a loop of phrases. 为此,我要遍历短语循环。 To run this loop continuously, I first call the loop, then I call it again with setInterval timed to start when the initial loop ends. 为了连续运行此循环,我首先调用该循环,然后使用setInterval再次调用该循环,定时将在初始循环结束时开始。 Here is the code I've got, which works even if it isn't what could be considered quality code: 这是我得到的代码,即使它不是质量代码也可以使用:

function loop(){

for (let i = 0; i < header_phrases.length; i++){
    (function (i) {
        setTimeout(function(){
          header_txt.textContent =  header_phrases[i];
        }, 3000 * i);
    })(i);
};
}

loop();
setInterval(loop, 21000);

Is there a better way to right this code for both performance and quality? 有没有更好的方法可以同时针对性能和质量使用此代码? Do I need to use async? 我需要使用异步吗? If so, any material I can see to learn more? 如果是这样,我可以看到任何资料以了解更多信息? Thanks! 谢谢!

You can implement the same logic using recursion. 您可以使用递归来实现相同的逻辑。

function recursify(phrases, index = 0) {
    header_txt.textContent = phrases[index];
    setTimeout(function () {
        recursify(phrases, index < phrases.length - 1 ? index + 1 : 0);
    }, 300)
}

recursify(header_phrases);

The function 'recursify' will call itself after 300 miliseconds, but everytime this function gets called, the value of index will be different. 函数“ recursify”将在300毫秒后调用自己,但是每次调用此函数时,index的值都会不同。

If I understand your requirement correctly, you want top populate an element from an array of values. 如果我正确理解您的要求,则希望从值数组中填充一个元素。

A simple way to do this is: 一种简单的方法是:

doLoop();
function doLoop() {
    var phraseNo=0;
    setTimeout(next,21000);
    next();
    function next() {
        header_txt.textContent =  header_phrases[phraseNo++];
        if(phraseNo>=header_phrases.length) phraseNo=0;
    }
}

This simply puts the next() function on the queue and waits. 这只是将next()函数放在队列上并等待。

The call to next() before the function simply starts it off without waiting for the timeout. 函数之前对next()的调用只是将其启动而无需等待超时。

this is assuming that header_txt and header_phrases are not global vars. 这是假设header_txtheader_phrases不是全局变量。 using global vars isn't a good idea. 使用全局变量不是一个好主意。

var repeatIn = 3000;
phraseUpdater();

function phraseUpdater() {
    var updateCount = 0,
        phrasesCount = header_phrases.length;

    setHeader();
    setTimeout(setHeader, repeatIn);
    function setHeader() {
        header_txt.textContent = header_phrases[updateCount++ % phrasesCount] || '';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM