如何无限期地循环遍历阵列并以角度显示值?
我有一系列的单词,我的目标是每隔几秒钟将其中的每个单词显示在HTML模板中。结果应该是这样的: https://bootstrapmade.com/demo/iportfolio/iportfolio/
知道可以通过使用以下JavaScript代码来进行操作:
const typed = select('.typed')
if (typed) {
let typed_strings = typed.getAttribute('data-typed-items')
typed_strings = typed_strings.split(',')
new Typed('.typed', {
strings: typed_strings,
loop: true,
typeSpeed: 100,
backSpeed: 50,
backDelay: 2000
});
}
我尝试使用Typescript复制相同的效果,然后写下以下代码:
export class HeroComponent implements OnInit {
words: string[] = ['marco', 'polo'];
word: string = "";
constructor() { }
ngOnInit(): void {
setTimeout(() => {
while(true){
for (let i=0; i < this.words.length; i++) {
setTimeout(() => {
this.word = this.words[i];
console.log(this.word)
}, 4000)
};
}}, 4000);
}
}
但是,一旦我运行了网站,它就说它的内存不足。
您是否可以提出一种聪明而优雅的方式来实现上面网站上链接的效果?
I have an array of words and my goal is to display each one of them in an HTML template every few seconds. The result should be something like this: https://bootstrapmade.com/demo/iPortfolio/
I know it is possible to do so by using the following JavaScript Code:
const typed = select('.typed')
if (typed) {
let typed_strings = typed.getAttribute('data-typed-items')
typed_strings = typed_strings.split(',')
new Typed('.typed', {
strings: typed_strings,
loop: true,
typeSpeed: 100,
backSpeed: 50,
backDelay: 2000
});
}
I tried to replicate the same effect using typescript and I wrote the following code instead:
export class HeroComponent implements OnInit {
words: string[] = ['marco', 'polo'];
word: string = "";
constructor() { }
ngOnInit(): void {
setTimeout(() => {
while(true){
for (let i=0; i < this.words.length; i++) {
setTimeout(() => {
this.word = this.words[i];
console.log(this.word)
}, 4000)
};
}}, 4000);
}
}
However once I run the website it says that it runs out of memory.
Would you be able to suggest a smart and elegant way to achieve the effect linked in the website above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将是这样做的方法:
然后使用html中的异步管:
示例: https://stackblitz.com/edit/edit/angular-ivy-7xbuft?file=src/app/app/app/app.component.ts
Timer
从第一个参数(0ms)开始,以增加顺序(0、1、2、3,...)以越来越多的顺序发射整数,然后每次由第二个参数(4000ms)给出的每个间隔。管道
让我们可以在返回之前对任何发射值执行一系列操作。MAP
取出发射值并返回不同的值,在这种情况下,我们使用整数计算数组的索引,然后在该索引处返回单词。async
管道将在销毁组件时订阅可观察到的可观察情况并取消订阅,以停止执行。取消订阅对于防止内存泄漏很重要。This would be the way to do it:
Then use the async pipe in html:
Example: https://stackblitz.com/edit/angular-ivy-7xbuft?file=src/app/app.component.ts
timer
from RxJS emits integers in increasing order (0, 1, 2, 3, ...) starting at the first parameter (0ms) and then once every interval given by the second parameter (4000ms).pipe
lets us perform a series of actions on any emitted value before returning.map
takes the emitted value and returns a different value, in this case we use the integer to calculate the index of the array, then return the word at that index.The
async
pipe will subscribe to the observable and unsubscribe when the component is destroyed, stopping the execution. Unsubscribing is important to prevent memory leaks.