+ 6

[Solved] Q. SetInterval() or SetTimeout()?

I have a multi-line string of the text to be displayed. I have a multi-line string of timings for the text to be displayed. I want to display the lines of text string line by line with different timings which I mentioned in timings string. I don't want to write SetInterval again and again for each line. How can it be done?

29th Dec 2021, 5:10 AM
Shobhit :)
Shobhit :) - avatar
7 ответов
+ 11
var text = `Hi Hello Bye`; var time = `1000 2000 3000`; time=time.split("\n").map(v=>parseFloat(v)); text.split("\n").forEach((v,i)=>{ setTimeout(()=>{ document.write("<br>"+v); },time[i]); });
29th Dec 2021, 11:40 AM
SAN
SAN - avatar
+ 6
Shobhit, Is it something like this?? https://code.sololearn.com/WetwsKsFeCG8/?ref=app
29th Dec 2021, 11:05 AM
Ipang
+ 4
Ipang The example code is as follows:- var text = `Hi Hello Bye`; var timings = `1500 250 5000`; var textBox = document.getElementById("textBox"); var splittedText = text.split(/' '/); var splittedTimings = timings.split(/' '/); let x = 0; setInterval(() => { textBox.innerHTML = splittedText[x]; x++; }, x);
29th Dec 2021, 9:06 AM
Shobhit :)
Shobhit :) - avatar
+ 4
Ipang I have different timings for different text lines. Which I want to synchronize with the timings. What I mean is that I want to display text lines in different time Intervals.
29th Dec 2021, 9:09 AM
Shobhit :)
Shobhit :) - avatar
+ 3
Hello Shobhit, I didn't really get the timing string concept, can you please elaborate further, maybe how the string is formatted, or how it is used with setTimeout() ...
29th Dec 2021, 6:39 AM
Ipang
+ 2
Thanks alot SAN and Ipang for your answers. 🤩🤗
29th Dec 2021, 11:52 AM
Shobhit :)
Shobhit :) - avatar
0
How do I send messages?
30th Dec 2021, 3:17 PM
ABOxTRKi
ABOxTRKi - avatar