javascript - How can I use a default value for this array? The result is not displayed until the first loop -
i have code:
<span id="changetext"></span> <script type="text/javascript"> var text = ["cool", "awesome", "outstanding"]; var counter = 0; var elem = document.getelementbyid("changetext"); var refreshintervali = setinterval(change, 2000); function change() { elem.innerhtml = text[counter]; counter++; if (counter >= text.length) { clearinterval(refreshintervali); } } </script>
the output correctly displays each word after 2000ms, takes 2sec display first word. how can set default value displayed until start of loop?
thanks in advance :)
why not this
var text = ["cool", "awesome", "outstanding"]; var counter = 1; var elem = document.getelementbyid("changetext"); elem.innerhtml = text[0]; var refreshintervali = setinterval(change, 2000); function change() { elem.innerhtml = text[counter]; counter++; if (counter >= text.length) { clearinterval(refreshintervali); } }
set first value of array innerhtml
on load , let counter start @ 1
, should work intended.
Comments
Post a Comment