+ 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?
7 Answers
+ 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]);
});
+ 6
Shobhit,
Is it something like this??
https://code.sololearn.com/WetwsKsFeCG8/?ref=app
+ 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);
+ 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.
+ 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() ...
0
How do I send messages?