I need some text to change (with a 1 second interval) on my website. (https://website.elliott23.repl.co). I can’t figure out how to make it routinely change with an interval of 1 second. This is my javascript for it.
var $text = $("#text");
var numbers = ["First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"];
for (var i = 1; i <= 10; ++i) {
(function(index) {
setTimeout(function() {
$text.html(numbers[index-1] + " sentence");
}, i * 1000);
})(i);
}
This is my HTML
<div id="text"></div>
The text isn’t changing. This is the html all around the element that needs to change.
<div class="opening">
<h1>Hey, I'm<span class="green-text"> Elliott D'Orazio</span><br></h1>
<h1>I design and develop<span class="italic"></span><div id="text"></div></h1>
</div>
I tried already checking on stack overflow. None of the articles helped me do what I needed to do. The code I provided above is from another stack post. I have no clue how to do this as my JavaScript knowledge is less than my HTML and CSS.
3
Answers
If you need to do this periodically you can use setInterval instead of setTimeout and for loop.
Something like this:
In addition I recommend to change ID from text, to something more unique.
Make sure you add JQuery to your page and then you can change your code to this:
Make sure to load jQuery because it is not vanilla JS. Simply add the library’s script, e.g. from a CDN: