/    Sign up×
Community /Pin to ProfileBookmark

delay for loop?

What would be the proper way to delay a for loop from counting? Everything I have tried seems to freeze and crash the browser. I’m looking to put a 10 second delay before each increment of the count variable. Any suggestions?

Sorry i mistyped in the search field issue resolved

[URL=”http://www.webdeveloper.com/forum/showthread.php?t=107364″]http://www.webdeveloper.com/forum/showthread.php?t=107364[/URL]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@NatdripSep 21.2007 — you don't use a for loop.

your for loop is the setInterval or setTimeout

every so many seconds it will fire off the functions called

so it acts as a delay.

use a global variable to determine when to stop.
Copy linkTweet thisAlerts:
@KorSep 21.2007 — Here's an example on using [B]setInterval[/B] method:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
var n,setT;
function setStart(){
var delayC=10000;
n=1;
setT=setInterval('startCount()',delayC)
}
function startCount(){
var stopC=10;
if(n&lt;=stopC){
document.getElementById('mydiv').firstChild.data=n;
n++
}
else{clearInterval(setT)}
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="mydiv"&gt;0&lt;/div&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="cursor:pointer" onclick="setStart()"&gt;start counting&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;

&lt;span style="cursor:pointer" onclick="setT?clearInterval(setT):null"&gt;stop counting&lt;/span&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @pentace spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...