|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
settimeout problem in two nested loops
hi im trying to make a input box have its value changed to several texts got from a different text area from time to time..and before the value of the input box changes i want to use a counter in a div to show countdown and slow down change of text and bgcolor of the div with the countdown in it....but im totally lost...the inputs boxes value changes before it even goes to the loop for changing the number in the div and the number doesn't change from time to time it just waits for the given time and goes straight to the last number and thats it...
here is the code Code:
<head>
<script type="text/javascript">
<!--
function timer(num){
counter = document.getElementsByTagName('div')(0)
newtext = document.createTextNode(num);
counter.removeChild(counter.firstChild);
counter.appendChild(newtext);
changebgcolorofdiv();
}
function changebgcolorofdiv(){
var divcolor = document.getElementsByTagName('div')(0).style.back groundColor;
var divcolor2 = divcolor.split('#');
divcolor3 = parseInt(divcolor2[1],16);
divcolor3 = divcolor3+10;
document.getElementsByTagName('div')(0).style.back groundColor = divcolor3;
}
//-->
</script>
</head>
<body>
<script type="text/javascript">
<!--
function changetext(){
var giventext = textarea1.value.split('\n');
for(i=0;i<hackpass.length;++i){
document.textbox2.value = giventext; //Here im trying to change the text in a loop
for(n=0;n<10;n++){
setTimeout(timer(n),5000);//here im trying to slow down this loop and the parent loop before the text is changed in the textbox2 and to make the countdown
}
}
}
//-->
</script>
<a href="javascript:changetext();"> Changetext</a><!--link to changetext()-->
<div name="counter" style="margin-top: 20px;background-color:aaaaaa; border-style:solid; border-width:1px; border-color: 225500; width: 100px; height: 100px"></div><!--Div for the countdown to show-->
<input type="text" name="textbox2" size=20><br><br><!--this is the input box where the text should be changed a short time after time with delay-->
<textarea name="textarea1"></textarea><br><br><!--textarea where i can enter the texts i wish to show in the textbox2 time after time with a delay-->
</body>
Looking forward to your help ![]() |
|
#2
|
||||
|
||||
|
setTimeout
this code
for(n=0;n<10;n++){ setTimeout(timer(n),5000); } is resetting the setTimeout with each pass through the loop. The loop is being passed through before the setTimeout has a chance to "time". It's OK to call the timer function inside the changetext function, but the setTimeout should call the function it's inside of ie. setTimeout(changetext(),5000); |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > settimeout problem in two nested loops |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|