Connecting Tech Pros Worldwide Help | Site Map

onclick only works once

Steve
Guest
 
Posts: n/a
#1: Nov 20 '08
This simple script works first time but if you click a second time on
the button nothing happens. I expected that the roll function would be
executed again but it doesn't. Could someone explain why.

http://members.chello.at/stephen.jou...hotspots1.html

javascript is at
http://members.chello.at/stephen.jou...e/hotspots1.js

Thanks in advance
Conrad Lender
Guest
 
Posts: n/a
#2: Nov 20 '08

re: onclick only works once


On 2008-11-20 22:00, Steve wrote:
Quote:
This simple script works first time but if you click a second time on
the button nothing happens. I expected that the roll function would be
executed again but it doesn't. Could someone explain why.
function roll() {
^^^^
for(var i=0; i<10; i++) {
var roll = Math.floor(Math.random()*6+1);
^^^^

Oops :-)


- Conrad
Conrad Lender
Guest
 
Posts: n/a
#3: Nov 20 '08

re: onclick only works once


On 2008-11-20 22:10, Conrad Lender wrote:
Quote:
Oops :-)
Err, scratch that. Sorry.

for(var i=0; i<10; i++) {
var roll = Math.floor(Math.random()*6+1);
rolls.push(roll);
^^^^^^^^^^
}
for(var i=0; i<10; i++) {
var dice="die" + i;
document.getElementById(dice).innerHTML=rolls[i];
^^^^^

You're pushing new values to the rolls array, but always read the same
10 values from the start.


- Conrad
Gregor Kofler
Guest
 
Posts: n/a
#4: Nov 20 '08

re: onclick only works once


Steve meinte:
Quote:
This simple script works first time but if you click a second time on
the button nothing happens. I expected that the roll function would be
executed again but it doesn't. Could someone explain why.
>
http://members.chello.at/stephen.jou...hotspots1.html
>
javascript is at
http://members.chello.at/stephen.jou...e/hotspots1.js
>
Thanks in advance
You are constantly pushing values onto the rolls array, and feeding the
output always with the same first few entries. A quick run with a
debugger (e.g. Firebug) would have revealed the problem.

A

rolls = [];

at the beginning of the roll function should suffice. (The rest of the
code is ..er... could be better.)

Gregor
Steve
Guest
 
Posts: n/a
#5: Nov 20 '08

re: onclick only works once


On Nov 20, 10:17*pm, Conrad Lender <crlen...@yahoo.comwrote:
Quote:
On 2008-11-20 22:10, Conrad Lender wrote:
Quote:
>
You're pushing new values to the rolls array, but always read the same
10 values from the start.
>
Thanks Conrad.

rf
Guest
 
Posts: n/a
#6: Nov 21 '08

re: onclick only works once



"Steve" <stephen.young@chello.atwrote in message
news:24fa0$4925cfd0$3eb29844$23511@news.chello.at. ..
Quote:
This simple script works first time but if you click a second time on the
button nothing happens. I expected that the roll function would be
executed again but it doesn't. Could someone explain why.
It is being executed again. You are just putting exactly the same numbers
back there.


Closed Thread