/    Sign up×
Community /Pin to ProfileBookmark

Random quote every 2 minutes

Hi I did a search but didn’t get this so sorry if I double posted.
I’m calling an external quote.js file for a random quote which is basic code as follows:

var quotes=new Array()
quotes[0]=’something’
quotes[1]=’something else’
(etc until)
var whichquote=Math.floor(Math.random()*(quotes.length))
document.write(quotes[whichquote])

Simple enough. What I want to do is call the external file every two minutes (without refreshing the page)or change the file itself so I get a random quote every two minutes. What do I have to do to the .js file and/or the call function to get this to happen?
Sorry if it’s obvious stuff I’m kind of new at this.
Thank you for your help

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@CharlesAug 16.2004 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Example</title>

<script type="text/javascript">

<!--

Date.ONE_SECOND = 1000;

Array.prototype.random = function () {return this[Math.floor(Math.random() * this.length)]}

decalogue = new Array();

decalogue[0] = 'I am the Lord your God who brought you out of bondage. You shall have no other gods but me.'

decalogue[1] = 'You shall not make for yourself any idol.'

decalogue[2] = 'You shall not invoke with malice the Name of the Lord your God.'

decalogue[3] = 'Remember the Sabbath Day and keep it holy.'

decalogue[4] = 'Honor your father and your mother.'

decalogue[5] = 'You shall not commit murder.'

decalogue[6] = 'You shall not commit adultery.'

decalogue[7] = 'You shall not steal.'

decalogue[8] = 'You shall not be a false witness.'

decalogue[9] = 'You shall not covet anything that belongs to your neighbor.'

// -->

</script>

</head>

<body>

<script type="text/javascript">

<!--

document.write('<p id="decalogue">', decalogue.random(), '</p>')

if (document.getElementById) setInterval ("document.getElementById('decalogue').firstChild.data = decalogue.random()", 2 * Date.ONE_SECOND)

// -->

</script>

</body>

</html>[/font]
Copy linkTweet thisAlerts:
@TygerTygerauthorAug 17.2004 — Very nice just what I wanted thank you.

I don't suppose there is a way to prevent it doing the same one twice in a row is there? Like go through 0-9 once randomly without repeats, then do the same ad nauseum? It just looks a bit like it's finished when it does that the same one twice because I have quite a big gap. No worries if not, thanks for your help.
Copy linkTweet thisAlerts:
@mvivitAug 17.2004 — The simplest way to help avoid repeating quotes is to add more to your array. You can pick up lists of quotes from tons of places. We're using an array of 50 quotes. Either that or increase the interval between reloading the quote.
Copy linkTweet thisAlerts:
@TygerTygerauthorAug 17.2004 — Ok I have more quotes so thats cool but now I have another problem, sorry about this.

I have some & nbsp;'s in my quotes which don't show up on the first quote that displays, but they show up in all the other quotes displayed after the first one. I'd rather use positioning formatting instead of & nbsp; but i can't get it to work with the JS. Any ideas anyone?
Copy linkTweet thisAlerts:
@nitwitAug 17.2004 — No repeats.
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;untitled&lt;/title&gt;
&lt;style type="text/css"&gt;

body {
background: #000;
}
#foo {
font: normal 24px "comic sans ms";
color: #a9e;
margin: 100px;
}
#foo .n {
font-weight: bold;
color: #eba;
}


&lt;/style&gt;
&lt;script type="text/javascript"&gt;

var quotes = [
'This is quote &lt;span class="n"&gt;1&lt;/span&gt;. Hoohah.' ,
'This is quote &lt;span class="n"&gt;2&lt;/span&gt;. Deal with it.' ,
'This is quote &lt;span class="n"&gt;3&lt;/span&gt;. Yes, &lt;span class="n"&gt;3&lt;/span&gt;.' ,
'This is quote &lt;span class="n"&gt;4&lt;/span&gt;. Yet another quote.' ,
'This is quote &lt;span class="n"&gt;5&lt;/span&gt;. A nice one.' ,
'This is quote &lt;span class="n"&gt;6&lt;/span&gt;. Better than 5.' ,
'This is quote &lt;span class="n"&gt;7&lt;/span&gt;. No WMD.' ,
'This is quote &lt;span class="n"&gt;8&lt;/span&gt;. Feh.' ,
'This is quote &lt;span class="n"&gt;9&lt;/span&gt;. This is quote &lt;span class="n"&gt;9&lt;/span&gt;.' //no trailing comma!
];

var idx_array, idx_str;

function randomQ()
{
if (!/__RANDOM_QUOTE_ROTATOR__/.test(self.name))
{
var idx, i, ind1, ind2, temp, arraylength = quotes.length;
idx_array = new Array(arraylength);
for (i = 0; i &lt; arraylength; ++i)
idx_array[i] = i;
for (i = 0; i &lt; arraylength; ++i)
{
ind1 = Math.floor(Math.random() * arraylength);
ind2 = Math.floor(Math.random() * arraylength);
temp = idx_array[ind1];
idx_array[ind1] = idx_array[ind2];
idx_array[ind2] = temp;
}
idx_str = idx_array.join(',');
}
else idx_str = self.name.split('__RANDOM_QUOTE_ROTATOR__')[1];

<i> </i>idx_array = idx_str.split(',');
<i> </i>var first_idx = idx_array.splice(0, 1);
<i> </i>document.getElementById('foo').innerHTML = quotes[first_idx]; //element id
<i> </i>idx_array[idx_array.length] = first_idx;
<i> </i>idx_str = idx_array.join(',');
<i> </i>self.name = '__RANDOM_QUOTE_ROTATOR__' + idx_str;
}

onload = function()
{
randomQ();
setInterval('randomQ()', 3000); //number of seconds * 1000
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="foo" title="quote"&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@CharlesAug 17.2004 — Aack! The Element.innerHTML property is a Microsoft corruption of JavaScript. Every time you use it the Dark Lord grows just a little bit stronger - and you risk your script failing for more users than necessary.

You have two options but both work because the quotes are JavaScript and not HTML. You can use "xA0" instead of "&amp;nbsp;" or you can just change that "P" element into a "PRE" element.

[font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Example</title>

<style type="text/css">

<!--

pre {font-family:Georgia, sans-serif}

-->

</style>

<script type="text/javascript">

<!--

Date.ONE_SECOND = 1000;

Array.prototype.random = function () {return this[Math.floor(Math.random() * this.length)]}

decalogue = new Array();

decalogue[0] = '*tI am the Lord your God who brought you out of bondage. You shall have no other gods but me.'

decalogue[1] = '*
tYou shall not make for yourself any idol.'

decalogue[2] = '*tYou shall not invoke with malice the Name of the Lord your God.'

decalogue[3] = '*
tRemember the Sabbath Day and keep it holy.'

decalogue[4] = '*tHonor your father and your mother.'

decalogue[5] = '*
tYou shall not commit murder.'

decalogue[6] = '*tYou shall not commit adultery.'

decalogue[7] = '*
tYou shall not steal.'

decalogue[8] = '*tYou shall not be a false witness.'

decalogue[9] = '*
tYou shall not covet anything that belongs to your neighbor.'

// -->

</script>

</head>

<body>

<script type="text/javascript">

<!--

document.write('<pre id="decalogue">', decalogue.random(), '</pre>')

if (document.getElementById) setInterval ("document.getElementById('decalogue').firstChild.data = decalogue.random()", 2 * Date.ONE_SECOND)

// -->

</script>

</body>

</html>[/font]
Copy linkTweet thisAlerts:
@nitwitAug 17.2004 — Baloney. innerHTML predates W3C DOM, it's not a 'corruption' of anything, just a pre-standards attempt to make programming the client [i]easier[/i]. Here's a more tempered (and intelligent) [url="http://www.developer-x.com/content/innerhtml/"]colloquy[/url] on the subject.

For people unfamiliar with DOM 1+ who might want to insert rich HTML in a document, innerHTML can be a Godsend. I'd guess it's as well-supported, currently, as DOM interfaces. The above was not an endorsement, just a simple solution.
Copy linkTweet thisAlerts:
@CharlesAug 17.2004 — The DOM level 1 spec included all the old client side JavaScript Document Object Model and is often refered to as DOM level 0. And note, Element.innerHTML is not included as a part of DOM level 0. It was always just something that Microsoft came up with.

I will, however, agree that it is extremely useful for anything complicated - and you're not supposed to make a JavaScript dependant web page anyway. But there is no need to go there for such simple tasks.
Copy linkTweet thisAlerts:
@TygerTygerauthorAug 17.2004 — Thanks very much guys, they both work a treat. I know nothing about .innerHTML, but I went with Charles's code because it was more compressed.
Copy linkTweet thisAlerts:
@nitwitAug 17.2004 — Then why did you post this?I don't suppose there is a way to prevent it doing the same one twice in a row is there?[/QUOTE]That takes more coding, less 'compressed'. It's called a 'feature'.

Charles: there are lots of other things that MS just 'came up with' that [i]are[/i] part of the current specification. Just because they did it, doesn't mean it's bad. OK, maybe it is. Not worth arguing about...?
Copy linkTweet thisAlerts:
@TygerTygerauthorAug 17.2004 — If you read mvivits post and then mine you will see that I added more quotes instead of rescripting. I did say so.
×

Success!

Help @TygerTyger 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 5.5,
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,
)...