Forum Moderators: open

Message Too Old, No Replies

Looking for code to show a random number between two numbers

for a pseudo whos-on counter

         

outsourcery

1:33 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



I want to be able to show a figure between say 1 and 100 randomly over a period of minutes, as a sort of "who's on the site" counter, until I get the real one working with the next site revamp.

Does anyone have any ideas?

JH

Bernard Marx

1:58 pm on Jul 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wholeheartedly defend your right to have no integrity :)

<html>
<head><title>Random Integer</title>
<script>

var randMin = 10
var randMax = 100
var minutes = 3
function getRandomInt(min,max)
{
return Math.round(min + Math.random()*(max-min))
}

window.onload = function()
{
setInterval
(
"document.getElementById('targ').innerHTML = getRandomInt(randMin,randMax)",
minutes*60000
)
}

</script>
</head>
<body>

<p id="targ">Hello</p>

</body>
</html>

outsourcery

2:31 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



So how does that work then? I've tried it and all I get is Hello.

DrDoc

3:04 pm on Jul 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld! :)

You could also handle this on the server using PHP/Perl/ASP

Mr_Brutal

3:10 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



Try setting the timer to minutes*100

Its very funky!

DrDoc

3:22 pm on Jul 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try setting the timer to minutes*100

Intervals are in milliseconds... Thus, 60*1000/second... Thus the "minutes*60000" part ;) I suggest that is left as-is.

Bernard Marx

3:24 pm on Jul 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, sorry. Didn't have 3 minutes to hang about. Fair enough.
I suppose it would be more correct to set minutes to some fraction, if you want instant action.

var minutes = 0.1

Come to think of it, this shouldn't be on a timer at all. To emulate a server-side thingy, it should just give one random figure per page visit/refresh.

outsourcery

3:29 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



As a dim person of no integrity, I still don't understand how this works. All I get is "hello"

:(

Mr_Brutal

3:34 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



:) I just meant change it to something smaller to prove it was working! If you use the script as above you'll have to look at 'Hello' for 61 seconds before it changes to your random number!

The script changes the Hello text to the random number after this time. Have you waited for a full minute - (without refreshing)?

DrDoc

3:52 am on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe your browser doesn't support getElementById? Which browser are you using?

outsourcery

2:38 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



MYie2. I'll try it in Netscape just now.

Bernard Marx

2:58 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IE2?!?! Maybe I should go to work on my backward-compatibility.

outsourcery

12:58 pm on Jul 19, 2004 (gmt 0)

10+ Year Member



Try this [myie2.com...]

It's not IE2. It's better.

Bernard Marx

1:18 pm on Jul 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I should hope so!