gpt4 book ai didi

javascript - 停止无限 Javascript 循环

转载 作者:行者123 更新时间:2023-12-03 03:40:58 25 4
gpt4 key购买 nike

我似乎无法让以下代码停止无限循环。我对 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com