Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

time delays

Status
Not open for further replies.

Toe

Programmer
Oct 26, 2001
71
GB
I have a loop in a program that I want to slow down. i mean litterally slow down the operation of the code so that it stops for a length of time before executing the next step.

all the info I find pertains to settin timers (setTimeout) but all this actually does is delay the whole process. (I have a loop and i want it to loop slower. setting a timer just creates a load of defferred statements)

I was thinking about setting a flag with the timer called function resettign it but had problem with it thinking the variable was local and not the same one.

any ideas?
 

You can't slow down or pause execution in JavaScript.

You can emulate it, as you've found, with setTimeout, but there's no 'real' way to slow loops down.

Sorry!

Dan
 
You could put a "for" loop in your scripot where yopu want to "slow" the script down. You could count from 1 to say a million (you'll have to experiment here):

for (x=1,x<=1000000,x++);

Remember - the actual time till termination will vary for each computer depending on processor speed.

There's always a better way. The fun is trying to find it!
 
tviman,

I used to do that all the time back when I was in high school coding video games in qbasic. Unfortunately none of them are playable anymore cause they run at light speed :)

-kaht

banghead.gif
 
Code:
setTimeout()
will let you slow down a routine but you have to restructure the program a bit. Cut the logic in question into two pieces, the first including everything up to and including the loop code. Where the loop terminates insert a
Code:
setTimeout()
that calls the second part after an acceptable delay. Something like this --
Code:
function main() {
  someProcessing();
  for (var j = 0; j < someNumber; j++) {
    someLoopProcessing(j);
    }
  someMoreProcessing();
}
becomes --
Code:
function main1() {
  someProcessing();
  for (var j = 0; j < someNumber; j++) {
    someLoopProcessing(j);
    }
  setTimeout(&quot;main2();&quot;,iDelay);
}

function main2() {
  someMoreProcessing();
}
 
thanks wray.

i had actually come to a similar conclusion myself last night - what I am doing is moving a layer across the screen.
i realised that what I needed to do was have a function DoNextMove that calls itself via a timer until it has reached the end point .

thanks everyone for their suggestions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top