Click to See Complete Forum and Search --> : Speed Problem


IceMetalPunk
01-10-2004, 03:46 PM
Hi,
I have a page, with all of this code EXACTLY as it is shown:

<title> </title>
<script>
letter=0;
next=0;
ud="+";
num=0;
message=new Array();
function AddMessages() {
if (isNaN(document.j.number.value*1) || document.j.number.value=="" || document.j.number.value==" ") { alert("Please type a NUMBER in the box."); return; }
document.getElementById("boxes").innerHTML="";
num=document.j.number.value*1;
for (i=0; i<num; i++) { document.getElementById("boxes").innerHTML+="<input type='text' name='message"+i+"'><br>"; }
}
function DoMessage() {
eval("letter"+ud+ud);
if (next>message.length-1) { next=0; }
if (letter>message[next].length) { ud="-"; }
if (letter<1) { ud="+"; next++; }
document.getElementById("type").innerHTML=message[next].substring(0, letter)+"_";
setTimeout("DoMessage()",100);
}
function DoIt() {
for (i=0; i<num; i++) { message[i]=eval("document.j.message"+i+".value"); }
}
</script><form name="j"><center><div id="boxes"><input type='text' name='message0'><br></div><br><input name="number" maxlength=2> <input type="button" value="Adjust Message Number" onClick="AddMessages()"> <input type="button" value="Make Message Typer!" onClick="next=0; letter=0; DoIt(); DoMessage()"><P><div id="type"></div>

It is made so that the user can enter as many messages as they want (up to 99) and it will "type" them and rotate them. The problem I'm having is that if they change a message and click "Make Message Typer!" again, it runs the script twice and speeds up. I have tried using a variable speed, and doubling that number each time the button is clicked, but that makes it jumpy. Anyone know how to fix this? Thanks in advance.

-IceMetalPunk (Kevin ;) )

P.S. The site is attatched as a .txt file in case it is too hard to read in this post.

David Harrison
01-10-2004, 08:15 PM
Hi there Ice.

I've modified your code extensively, however the page still looks exactly the same as before. It is now valid HTML 4.01 transitional.

I have built into it a more advanced number checker, it uses regular expressions to remove any non-numbers, then if there is nothing left it gives num a default value of 1.

I have also removed all instances of eval from the script, this is because it is generally considered bad practice to use eval. I don't really see why, and people have tried to explain it to me but I don't really get it.

There is an option at the top of the script for whether you want the message write script to loop infinitely, ie: once the last word has been display it jumps back to the start and loops through them again. The default is for it to only loop once however you can change that if you want. Look for this:

var loop=false;

I have moved some of the stuff that you had in an onclick event handler into the first function that you have set to run, just to get it out of the way really. I have also added a variable that is used to prevent the script from running twice if the user clicks the button twice. I have changed the type of variable for ud, it is now a Booleon variable (true/false variable) and it just alternates between the two.

In the script that you posted, once the end of the message loop was reached it caused an error, this has now been corrected.

All in all, the script should now work as you want it to and run error free. Hope you like it. :)

TheBearMay
01-11-2004, 02:40 PM
Haven't checked the changes lavalamp made, but if you clear the timeOut before resetting it you should eliminate the possibility that you have multiple timers running.

IceMetalPunk
01-14-2004, 04:33 PM
Lava,
You had some good ideas, but I would rather do as little modifying as possible. Thanks Bear, for the simple help.

-IceMetalPunk;)

David Harrison
01-15-2004, 03:20 AM
OK, but you should know that when your script has finished running it causes an error. That error also prevents it from letting the user run the script again.