/    Sign up×
Community /Pin to ProfileBookmark

Help coding text display

I found this code for randomly displaying text, but I want it to display the text strings in rotation instead of randomly.
Anyone help on coding this?

<html>
<head>
<title>Random Text</title>
<script type=”text/javascript”>
var textarray = [
“Notable Commanders, 3rd Armored Division”,
“Major General Alvin C. Gillem Jr., April 1941 to January 1942”,
“Major General Walton H. Walker, January 1941 to August 1942”,
“Major General Leroy R. Watson, August 1942 to August 1944”,
“Major General Maurice Rose, August 1944 to March 1945 KIA”,
“Brigadier General Doyle O. Hickey, March to June 1945”,
“Brigadier General Truman E. Boudinot, June & July 1945”,
“Brigadier General Frank A. Allen, Jr., July 1945”,
“Major General Robert W. Grow, July 1945 until inactivation”
// No comma after last entry
];
function RndText() {
var rannum= Math.floor(Math.random()*textarray.length);
document.getElementById(‘ShowText’).innerHTML=textarray[rannum];
}
onload = function() { RndText(); }
// Remove slashes from the next line if you want automatic rotation
var inter = setInterval(function() { RndText(); }, 2000);
</script>
</head>
<body>
<div style=”color:blue;” id=”ShowText”></div>
</body>
</html>

to post a comment

8 Comments(s)

Copy linkTweet thisAlerts:
@cootheadMar 18.2019 — Hi there k8OMS,

and a warm welcome to these forums.

Does this help...

``<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;untitled document&lt;/title&gt;

&lt;!--&lt;link rel="stylesheet" href="screen.css" media="screen"&gt;--&gt;

&lt;style media="screen"&gt;
body {
background-color: #f9f9f9;
font: 100% / 162% BlinkMacSystemFont, -apple-system, 'Segoe UI', roboto, helvetica, arial, sans-serif;
}
#showText {
color: #00f;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;div id="showText"&gt;
Notable Commanders, 3rd Armored Division
&lt;/div&gt;

&lt;script&gt;
( function() {
'use strict';
var delay = 3000,
c = 0,
textArray = [
'Notable Commanders, 3rd Armored Division',
'Major General Alvin C. Gillem Jr., April 1941 to January 1942',
'Major General Walton H. Walker, January 1941 to August 1942',
'Major General Leroy R. Watson, August 1942 to August 1944',
'Major General Maurice Rose, August 1944 to March 1945 KIA',
'Brigadier General Doyle O. Hickey, March to June 1945',
'Brigadier General Truman E. Boudinot, June &amp; July 1945',
'Brigadier General Frank A. Allen, Jr., July 1945',
'Major General Robert W. Grow, July 1945 until inactivation'
// No comma after last entry
];
setInterval(
function() {
c ++;
if ( c === textArray.length ) {
c = 0;
}
document.getElementById( 'showText' ).textContent = textArray[ c ];
}, delay );
}());
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;<i>
</i>
``


_coothead_
Copy linkTweet thisAlerts:
@rootMar 18.2019 — Hi, @coothead

Here is a tip I like when dealing with arrays that in themselves will be iterated over themselves again and a gain in a loop.

Array.prototype.loop = function(){
var r=this.shift(); // get the first entry.
this.push(r); // push it on the end of the array
return r; // return the item removed from the front
}
and running in a terminal gives Array.prototype.loop = function(){

var r=this.shift()

this.push(r);

return r;

}

var t=[7,8,9,1,2,3,4,5,6];

console.log("%s", t.toString());

console.log("%s", (t.loop()).toString());

console.log("%s", (t.loop()).toString());

console.log("%s", (t.loop()).toString());

console.log("%s", t.toString());

7,8,9,1,2,3,4,5,6

7

8

9

1,2,3,4,5,6,7,8,9[/quote]

And you never need have to have a variable counter. Useful if you have several arrays you need to have data looped indefinitely.

and this... textArray[(c++ % textArray.length)] will always return an element in the array without having to test if the counter is out of range as it will be a modulus of the array length and the remainder determines the array position, set c=0; as a global, then let her rip.

So the line...

could be distilled down to... setInterval(
function() {
document.getElementById( 'showText' ).textContent = textArray[(c++ % textArray.length)];
}, delay );
Copy linkTweet thisAlerts:
@cootheadMar 18.2019 — @root#1601896

Neat. 😀

_coothead_
Copy linkTweet thisAlerts:
@cootheadMar 18.2019 — Hi there k8OMS,

@root, our resident _JavaScript Guru_, suggests that it would be better coded like this...

``<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;untitled document&lt;/title&gt;

&lt;!--&lt;link rel="stylesheet" href="screen.css" media="screen"&gt;--&gt;

&lt;style media="screen"&gt;

body {
background-color: #f9f9f9;
font: 100% / 162% BlinkMacSystemFont, -apple-system, 'Segoe UI', roboto, helvetica, arial, sans-serif;
}
#showText {
color: #00f;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;div id="showText"&gt;
Notable Commanders, 3rd Armored Division
&lt;/div&gt;

&lt;script&gt;
( function() {
'use strict';
var delay = 3000,
c = 0, r,
textArray = [
'Notable Commanders, 3rd Armored Division',
'Major General Alvin C. Gillem Jr., April 1941 to January 1942',
'Major General Walton H. Walker, January 1941 to August 1942',
'Major General Leroy R. Watson, August 1942 to August 1944',
'Major General Maurice Rose, August 1944 to March 1945 KIA',
'Brigadier General Doyle O. Hickey, March to June 1945',
'Brigadier General Truman E. Boudinot, June &amp; July 1945',
'Brigadier General Frank A. Allen, Jr., July 1945',
'Major General Robert W. Grow, July 1945 until inactivation'
// No comma after last entry
];
setInterval(
function() {
Array.prototype.loop = function(){
r = this.shift(); // get the first entry.
this.push( r ); // push it on the end of the array
return r; // return the item removed from the front
}
document.getElementById( 'showText' ).textContent = textArray[( c++ % textArray.length )];
}, delay );
}());
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@k8OMSauthorMar 18.2019 — Thank You! Very much appreciated!
Copy linkTweet thisAlerts:
@cootheadMar 18.2019 — No problem, you're very welcome.

_coothead_
Copy linkTweet thisAlerts:
@rootMar 19.2019 — Well if you are using the document.getElementById( 'showText' ).textContent = textArray[( c++ % textArray.length )];, you wouldn't really need the prototype function, that is useful if you have an array that you want to have a copy of the element returned, so in that instance, it could be document.getElementById( 'showText' ).textContent = textArray.loop(); and then you have no need for the variable c altogether.
Copy linkTweet thisAlerts:
@cootheadMar 19.2019 — Hi there root,

using this...

``<i>
</i>&lt;script&gt;
( function() {
'use strict';
var delay = 3000,
textArray = [
'Notable Commanders, 3rd Armored Division',
'Major General Alvin C. Gillem Jr., April 1941 to January 1942',
'Major General Walton H. Walker, January 1941 to August 1942',
'Major General Leroy R. Watson, August 1942 to August 1944',
'Major General Maurice Rose, August 1944 to March 1945 KIA',
'Brigadier General Doyle O. Hickey, March to June 1945',
'Brigadier General Truman E. Boudinot, June &amp; July 1945',
'Brigadier General Frank A. Allen, Jr., July 1945',
'Major General Robert W. Grow, July 1945 until inactivation'
// No comma after last entry
];
setInterval(
function() {
document.getElementById( 'showText' ).textContent = textArray.loop();
}, delay );
}());
&lt;/script&gt; <i>
</i>
`</CODE>

...gives this information...

<CODE>
`<i>
</i>TypeError: textArray.loop is not a function<i>
</i>
``

Unfortunately, as javascript is not my forte, I have misunderstood your post

and require further guidance.

_coothead_
×

Success!

Help @k8OMS spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...