javascript - 停止无限 Javascript 循环

标签 javascript loops

我似乎无法让以下代码停止无限循环。我对 JS 相当陌生,在 codepen 上发现了这个。它对于我正在从事的项目非常有用,但前提是我能让​​它无限期地停止循环。在输入最后一个短语后让它停止是理想的。想法?

    <style>
    body {
        background-color:#333;
        text-align: center;
    }
    * { color:#fff; text-decoration: none;}
</style>
<script>
    var TxtType = function(el, toRotate, period) {
        this.toRotate = toRotate;
        this.el = el;
        this.loopNum = 0;
        this.period = parseInt(period, 10) || 2000;
        this.txt = '';
        this.tick();
        this.isDeleting = false;
    };

    TxtType.prototype.tick = function() {
        var i = this.loopNum % this.toRotate.length;
        var fullTxt = this.toRotate[i];

        if (this.isDeleting) {
            this.txt = fullTxt.substring(0, this.txt.length - 1);
        } else {
            this.txt = fullTxt.substring(0, this.txt.length + 1);
        }

        this.el.innerHTML = '<span class="wrap">'+this.txt+'</span>';

        var that = this;
        var delta = 200 - Math.random() * 100;

        if (this.isDeleting) { delta /= 2; }

        if (!this.isDeleting && this.txt === fullTxt) {
            delta = this.period;
            this.isDeleting = true;
        } else if (this.isDeleting && this.txt === '') {
            this.isDeleting = false;
            this.loopNum++;
            delta = 500;
        }

        setTimeout(function() {
            that.tick();
        }, delta);
    };

    window.onload = function() {
        var elements = document.getElementsByClassName('typewrite');
        for (var i=0; i<elements.length; i++) {
            var toRotate = elements[i].getAttribute('data-type');
            var period = elements[i].getAttribute('data-period');
            if (toRotate) {
                new TxtType(elements[i], JSON.parse(toRotate), period);
            }
        }
        // INJECT CSS
        var css = document.createElement("style");
        css.type = "text/css";
        css.innerHTML = ".typewrite > .wrap { border-right: 0.08em solid #fff}";
        document.body.appendChild(css);
    };
</script>

    <h1>
    <a href="" class="typewrite" data-period="2000" data-type='[ "Discover", "Learn", "Train", "Prepare", "Share" ]'>
        <span class="wrap"></span>
    </a>
</h1>

最佳答案

您可以通过检查 loopNum 是否仍然小于单词数组的长度来在最后一个元素之后停止它。

if(this.loopNum < this.toRotate.length){
    setTimeout(function() {
        that.tick();
    }, delta);
}

<style>
    body {
        background-color:#333;
        text-align: center;
    }
    * { color:#fff; text-decoration: none;}
</style>
<script>
    var TxtType = function(el, toRotate, period) {
        this.toRotate = toRotate;
        this.el = el;
        this.loopNum = 0;
        this.period = parseInt(period, 10) || 2000;
        this.txt = '';
        this.tick();
        this.isDeleting = false;
    };

    TxtType.prototype.tick = function() {
        var i = this.loopNum % this.toRotate.length;
        var fullTxt = this.toRotate[i];

        if (this.isDeleting) {
            this.txt = fullTxt.substring(0, this.txt.length - 1);
        } else {
            this.txt = fullTxt.substring(0, this.txt.length + 1);
        }

        this.el.innerHTML = '<span class="wrap">'+this.txt+'</span>';

        var that = this;
        var delta = 200 - Math.random() * 100;

        if (this.isDeleting) { delta /= 2; }

        if (!this.isDeleting && this.txt === fullTxt) {
            delta = this.period;
            this.isDeleting = true;
        } else if (this.isDeleting && this.txt === '') {
            this.isDeleting = false;
            this.loopNum++;
            delta = 500;
        }
        
        if(this.loopNum < this.toRotate.length){
          setTimeout(function() {
              that.tick();
          }, delta);
        }
    };

    window.onload = function() {
        var elements = document.getElementsByClassName('typewrite');
        for (var i=0; i<elements.length; i++) {
            var toRotate = elements[i].getAttribute('data-type');
            var period = elements[i].getAttribute('data-period');
            if (toRotate) {
                new TxtType(elements[i], JSON.parse(toRotate), period);
            }
        }
        // INJECT CSS
        var css = document.createElement("style");
        css.type = "text/css";
        css.innerHTML = ".typewrite > .wrap { border-right: 0.08em solid #fff}";
        document.body.appendChild(css);
    };
</script>

    <h1>
    <a href="" class="typewrite" data-period="2000" data-type='[ "Discover", "Learn", "Train", "Prepare", "Share" ]'>
        <span class="wrap"></span>
    </a>
</h1>

关于javascript - 停止无限 Javascript 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45623164/

相关文章:

javascript - 有没有办法模糊/突出显示单选按钮的文本?

php - 使用组迭代(循环)复杂的数字范围以生成括号表

javascript - 如何让我的 javascript 函数随机选择一个函数(幻灯片),执行它,然后重复?

c - 给定一个数组,找到小于 c 的 n 个数字的组合

jquery - DRY Jquery 选择下拉显示 div

PHP:如何结合 foreach 和 explode

javascript - Fetch 无法识别 102(或任何 1xx)状态代码

javascript - Protractor - 在给定端口上启动 Selenium 服务器

javascript - For 循环只运行一次

javascript - node.js 从 url 下载带有未知扩展名的图像