PDA

View Full Version : javascripts conflict? some help required please



pdsok
11-05-2006, 08:16 PM
Hi,
could anyone tell me why the lottery picker in the link below doesn't always work (does not stop the count), sometimes it does and sometimes it doesn't. In Firefox or IE

http://tstiger.org.uk/stuff.htm

I thought it might be var conflicts as there are some but i changed each in turn to no avail.

FYI, I am not a coder just a tinkerer, I can do html a bit and the website was done to help out and keep costs low, but I would like to keep the java on it if poss

ps yes i know the slider game works only in IE but the cadets like it and they all use IE :)

Thanks in advance

jscheuer1
11-05-2006, 11:01 PM
I would guess, especially if it is only a problem sometimes, that it has to do with the nature of timeouts. If one timeout is cancelling another timeout that is constantly being reissued, they would both have to fire at the right moments for this to work out in all cases.

A better strategy would be to have one be an interval. An interval is only issued once so, the precise timing of its cancellation wouldn't be so critical. I would remove this from the script:


T=setTimeout('lotto()',20);

add the variable T formally to the global scope but, use something a bit more unique, say Tlottovar. To do this, near the top of the script have:


/*****************************************
* Lottery Picker (By Kurt at kurt.grigg@virgin.net)
* Featured on/available at http://www.dynamicdrive.com/
* Modified by DynamicDrive.com for below config options
* This notice must stay intact for use.
*****************************************/

var totalnumbers=6 //input total numbers to generate
var lowerbound=1 //input lower bound for each random number
var upperbound=49 //input upper bound for each random number
var Tlottovar;

function lotto(){
. . .

Now, when you call lotto() from your button, it can look like so:


<input type="button" value="Lottery Number Picker" onclick="Tlottovar=setInterval('lotto()', 20);StOp();">

and your StOp() function would clear it like so:


function StOp(){
setTimeout('clearInterval(Tlottovar)',1400);
}

pdsok
11-06-2006, 09:57 AM
John,
Many thanks for that my friend
success,

I will now try to figure out why it works so I can do it myself another time

PdS