zenon905 Posted February 13, 2008 Share Posted February 13, 2008 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 More sharing options...
justsomeguy Posted February 13, 2008 Share Posted February 13, 2008 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 More sharing options...
zenon905 Posted February 13, 2008 Author Share Posted February 13, 2008 How would the code look like with setInterval? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 14, 2008 Share Posted February 14, 2008 How would the code look like? It would probably look similar to what you have, except it would use setInterval instead of setTimeout.http://www.w3schools.com/htmldom/met_win_setinterval.asp Link to comment Share on other sites More sharing options...
jeffman Posted February 14, 2008 Share Posted February 14, 2008 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 More sharing options...
jeffman Posted February 14, 2008 Share Posted February 14, 2008 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 More sharing options...
zenon905 Posted February 14, 2008 Author Share Posted February 14, 2008 What if I were to do it with document.write()? The document.getElementById added a textbox. I tried document.write but the n++ doesn't work when I do Link to comment Share on other sites More sharing options...
Synook Posted February 14, 2008 Share Posted February 14, 2008 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 More sharing options...
zenon905 Posted February 14, 2008 Author Share Posted February 14, 2008 How do I take away the text box? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 14, 2008 Share Posted February 14, 2008 The delete and backspace keys are both good choices. Link to comment Share on other sites More sharing options...
zenon905 Posted February 14, 2008 Author Share Posted February 14, 2008 I mean remove the text box and have the number appear on the page like the text in this post Link to comment Share on other sites More sharing options...
justsomeguy Posted February 14, 2008 Share Posted February 14, 2008 The code that Synook posted writes the number to an element on the page, in that case a div element. Link to comment Share on other sites More sharing options...
jeffman Posted February 14, 2008 Share Posted February 14, 2008 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 More sharing options...
Synook Posted February 15, 2008 Share Posted February 15, 2008 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 More sharing options...
zenon905 Posted February 15, 2008 Author Share Posted February 15, 2008 n=0function a(){ setInterval('document.getElementById("inf").value=n', 1000)} I made this code through what I can understand. How do I make n increase by 1 without changing too much of the code? I added n++ but it multiplies n by 2 each time. Link to comment Share on other sites More sharing options...
Synook Posted February 16, 2008 Share Posted February 16, 2008 n=0function a(){setInterval('n++; document.getElementById("inf").value=n', 1000)} Link to comment Share on other sites More sharing options...
zenon905 Posted February 16, 2008 Author Share Posted February 16, 2008 Thanks. Is there a way to have multiple setIntervals and have the script choose one randomly each time? Link to comment Share on other sites More sharing options...
Synook Posted February 17, 2008 Share Posted February 17, 2008 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 More sharing options...
zenon905 Posted February 17, 2008 Author Share Posted February 17, 2008 What does math.floor mean and can I make n++ increase more than 1 at a time? Link to comment Share on other sites More sharing options...
Synook Posted February 17, 2008 Share Posted February 17, 2008 What does math.floor meanMath.floor rounds a number down to an integer. E.g. 9.6 => 9 , 2342.45435 => 2342can 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 More sharing options...
zenon905 Posted February 17, 2008 Author Share Posted February 17, 2008 What about math.random Link to comment Share on other sites More sharing options...
Synook Posted February 17, 2008 Share Posted February 17, 2008 Creates a random float between 0 and 1, e.g. 0.6006193676079093, 0.8914675582931066 Link to comment Share on other sites More sharing options...
zenon905 Posted February 17, 2008 Author Share Posted February 17, 2008 setInterval('n++;document.getElementById("inf").value=n', 1000)setInterval('n+2;document.getElementById("inf").value=n', 1000) n+2 doesn't work. Link to comment Share on other sites More sharing options...
Ingolme Posted February 17, 2008 Share Posted February 17, 2008 setInterval('n++;document.getElementById("inf").value=n', 1000)setInterval('n+2;document.getElementById("inf").value=n', 1000) n+2 doesn't work. try n+=2. Link to comment Share on other sites More sharing options...
zenon905 Posted February 18, 2008 Author Share Posted February 18, 2008 It works thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.