Jump to content

Time Interval


zenon905

Recommended Posts

I am trying to do this: when I click a button, after 2 seconds it writes Number of infected files = A. A increases by 1 every 2 second interval. This is what I came up with

function a(){	var a=1	for(a=1;a<=10;a++){		setTimeout('document.write("Number of infected files" + a)',2000)}	}

But when I click the button this shows up after 2 secondssetTimeout('document.write("Number of infected files" + a)',2000)}}How do I make it work the way I planned?

Link to comment
Share on other sites

You named your function the same thing as a variable. When it prints "a" it prints the code of the function. But even if you change the name of the function or variable it's still not going to work because the variable you're trying to print isn't in the scope of the code that setTimeout runs. You should use a global variable if you want to do it like that. Also, it's not going to print that message once every two seconds, it's going to wait 2 seconds and then print the message 10 times instantly. That's because you have setTimeout in a for loop, so it runs through the for loop and sets 10 timeouts immediately, so timeout 1 comes after 2000 milliseconds, timeout 2 comes after 2001 milliseconds (or however many milliseconds it takes to run through the for loop once, which is actually less then one), then timeout 3 comes after 2002 milliseconds, etc. You're not scheduling something to run every 2 seconds, you are instantly scheduling 10 things to run 2 seconds after the loop runs, so they're still all going to run at the same time.If you want something to run every X seconds either use setInterval or have setTimeout run a function that will set the next timeout. Or, increment the timeout period each time you go through the loop so that the first timeout is scheduled for 2 seconds, then 4 seconds, then 6 seconds, etc.

Link to comment
Share on other sites

Here's how it might look if you called setTimeout recursively:

<body bgcolor="#ffffff">	   <input id='inf' />	   <script>		 var n = 1;		 function infected(){			 if (n <= 10){				 setTimeout('infected()',2000);			 }			 document.getElementById('inf').value = n;			 n++;		 }		 infected();	 </script> </body>

Link to comment
Share on other sites

And how it might look with setInterval. Basically, just rearranged.

<body>		 <input id='inf' />		 <script type="text/javascript">			 var myInterval = setInterval('infected()',2000);			 var n = 1;			 function infected(){				 if (n <= 10){					 document.getElementById('inf').value = n;				 }				 n++;			 }		 </script>	 </body>

Of course, either way, all you've built is a 2-second timer. I'll assume there's more to go with this, or that you're just experimenting. BTW, I put the changing value into an input because I really don't like the document.write() method. It's very 1990s, ya know? If you want the same effect, maybe better to update the innerHTML of a <div> in every function call. (Not that I'm a huge fan of innerHTML!)

Link to comment
Share on other sites

Document.write won't work, as it will clear the contents of a page if it is called after the page has loaded. Why not change that text box to a div and use innerHTML?

<body>		 <div id="inf"></div>		 <script type="text/javascript">			 var myInterval = setInterval('infected()',2000);			 var n = 1;			 function infected(){				 if (n <= 10){					 document.getElementById('inf').innerHTML = n;				 }				 n++;			 }		 </script>	 </body>

Link to comment
Share on other sites

This thread still cooking? Okay. I think what's below will do everything you originally tried to do.

<body>	 <div id="inf"></div>	 <script type="text/javascript">		 var myInterval = setInterval('infected()',2000);		 var n = 1;		 function infected(){			 if (n <= 10){				 document.getElementById('inf').innerHTML += "Number of infected files: " + n + " ";			 }			 n++;		 }	 </script> </body>

NOTE: I don't know for sure how IE feels about innerHTML += . FF likes it just fine.

Link to comment
Share on other sites

NOTE: I don't know for sure how IE feels about innerHTML += . FF likes it just fine.
IE feels good about it too :)
Link to comment
Share on other sites

Err? You could setInterval for a function which randomly does something each time:

function init() {setInterval("doRand()", 1000);}doRand() {var random = Math.floor(Math.random() * 2);if (random == 0) doSomething();if (random == 1) doSomethingElse();}

Link to comment
Share on other sites

What does math.floor mean
Math.floor rounds a number down to an integer. E.g. 9.6 => 9 , 2342.45435 => 2342
can I make n++ increase more than 1 at a time?
n = n + x; //Where x is the increment

Link to comment
Share on other sites

Creates a random float between 0 and 1, e.g. 0.6006193676079093, 0.8914675582931066

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...