Phalanxer
07-04-2012, 06:08 PM
I have been looking all around the net for a simple digestivable answer to what I'm looking for.
Can someone please give me a straight answer.. It would be very appreciated!
I just want to make JavaScript pause. I don't know what command to use. I tried this after trying to make sense of what social drop outs on other forums have said, but it didn't work.
<SCRIPT type="text/javascript">
document.bgColor = "red";
setTimeout(500);
document.bgColor = "black";
</SCRIPT>
nomanic
07-04-2012, 06:15 PM
The only real answer I can think of is splitting the commands into 2 functions, where function 1 is run, with a timeout to run function 2
<SCRIPT type="text/javascript">
function f1() {
document.bgColor = "red";
setTimeout('f2();',10000);
};
function f2() {
document.bgColor = "black";
};
f1();
</SCRIPT>
either way, 500 is not really much of a timeout anyways as 1000 is 1 second, you are pausing for half a second, you wouldnt notice it anyway
Phalanxer
07-04-2012, 06:23 PM
Sweet as man, thanks. How can you go about looping in JavaScript?... kinda like a while() operator in PHP..
nomanic
07-04-2012, 06:26 PM
you have while in javascript
Phalanxer
07-04-2012, 06:28 PM
Isn't there just a way to write like
flash
pause
flash
if you know what I mean?
nomanic
07-04-2012, 06:29 PM
you want to loop this background color change?
how many times? indefinately?
nomanic
07-04-2012, 06:48 PM
<SCRIPT type="text/javascript">
function f1() {
document.bgColor = "red";
setTimeout('f2();',10000);
};
function f2() {
document.bgColor = "black";
setTimeout('f1();',10000);
};
f1();
</SCRIPT>
nomanic
07-04-2012, 06:51 PM
To loop it n times try this-
<SCRIPT type="text/javascript">
function f1(f) {
document.bgColor = "red";
setTimeout('f2('+f+');',10000);
};
function f2(f) {
document.bgColor = "black";
f--;
if (f>0) {
setTimeout('f1('+f+');',10000);
}
};
f1(100);
</SCRIPT>
This will loop it 100 times
Phalanxer
07-04-2012, 07:00 PM
thanks man. I will look at it tomorrow when I am fresh. I've just finished an 10 hour PHP coding session. I'm cooked!
felgall
07-04-2012, 11:24 PM
See http://www.codingforums.com/showthread.php?t=263299 for a way to automate the code rewrite required where the pauses are in sequential code with no loops and nothing else to run after the current function.
Logic Ali
07-05-2012, 03:05 AM
Sweet as man, thanks. How can you go about looping in JavaScript?... kinda like a while() operator in PHP..
<script type="text/javascript">
(function ( c1, c2, t )
{
var state = false,
f = function(){ document.bgColor = ( state ^= true ) ? c1 : c2 };
setInterval( f, t );
})( 'black', 'red', 2000 );
</script>