zenon905 Posted February 27, 2008 Share Posted February 27, 2008 var int=setInterval("num++;document.getElementById('inf').value=num", 1000)I declared this variable but it runs right in the beginning when it is suppose to run after a few seconds. Link to comment Share on other sites More sharing options...
jeffman Posted February 27, 2008 Share Posted February 27, 2008 From the w3c docs I've read, I am not aware that the time of the first execution has been specified. Is it right away or is it after the interval? Could be at the brower's discretion. Or maybe I've missed something.To guarantee the performance you want, use setTimeout in a function and then call it recursively. That is, use setTimeout to call Function X, which calls setTimeout to call Function X . . . etc.setTimeout is guaranteed not to execute the assigned statements until the interval has expired. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 27, 2008 Share Posted February 27, 2008 setInterval shouldn't fire immediately, it should wait until the first interval for the first execution. So that code should run every second. Make sure you initialize the num variable to 0 first (or whatever initial value). Link to comment Share on other sites More sharing options...
jeffman Posted February 27, 2008 Share Posted February 27, 2008 Well, maybe I should have run some tests before answering.FF/Mac and Safari work as you say. I made certain of this by initiating setInterval with a button click and keeping the interval 5 seconds long. No mistake. Both those browsers wait the full interval before executing the first time.I'll reserve further judgment till I've seen additional docs (my burden, not yours).ZENON:Do you call setInterval in a function or just inline in your script? 1000 milliseconds is just one second, not several. If the call is inline, much of that time could get eaten up during the loading of your document. You probably thought of that, but in case you hadn't . . . Link to comment Share on other sites More sharing options...
jesh Posted February 27, 2008 Share Posted February 27, 2008 Also, a little short cut - feel free to ignore this post.This: num++; document.getElementById('inf').value = num; is equivalent to: document.getElementById('inf').value = ++num; Link to comment Share on other sites More sharing options...
jeffman Posted February 28, 2008 Share Posted February 28, 2008 document.getElementById('inf').value = ++num; C# my @$$. You're C hacker pure as the driven snow, am I right? Or you studied with one. I'll bet you even recognize this old gem:while (*x++ = *y++);If you're old enough, you might even yearn for the days when it was standard practice to hide flags and other cool stuff inside unused bit number 8."Why would anyone need more than 64K of ram?" Link to comment Share on other sites More sharing options...
zenon905 Posted February 28, 2008 Author Share Posted February 28, 2008 I just want to give that code the int variable. I followed the clock example but it didn't work. Link to comment Share on other sites More sharing options...
jeffman Posted February 28, 2008 Share Posted February 28, 2008 Did you follow justsomeguy's advice? He's saying you have to add a line like this before you can use num:var num = 0;Otherwise you just generate an error every second. Link to comment Share on other sites More sharing options...
zenon905 Posted February 28, 2008 Author Share Posted February 28, 2008 Other question what's the document. code for making the background an image from my desktop? Link to comment Share on other sites More sharing options...
jeffman Posted February 29, 2008 Share Posted February 29, 2008 Other question what's the document. code for making the background an image from my desktop?Sorry. I don't understand. You want a file on your desktop to be the background of your web document? Are you serving from your desktop computer or a provider? Link to comment Share on other sites More sharing options...
zenon905 Posted February 29, 2008 Author Share Posted February 29, 2008 I made a file and I want it to be my background. Is there a javascript code for making the background the image of a file? Like document.bgimage or something. Link to comment Share on other sites More sharing options...
jeffman Posted February 29, 2008 Share Posted February 29, 2008 In java script: yourObject.style.backgroundImage="url(image.jpg)";In CSS: yourClass { background-image: url(image.gif); } Link to comment Share on other sites More sharing options...
zenon905 Posted March 1, 2008 Author Share Posted March 1, 2008 What is yourObject? Link to comment Share on other sites More sharing options...
boen_robot Posted March 1, 2008 Share Posted March 1, 2008 What is yourObject?The element object which you want to have the certain background. For example:document.getElementById('inf').style.backgroundImage="url(image.jpg)"; will apply the background image on the element with an ID "inf". Link to comment Share on other sites More sharing options...
zenon905 Posted March 2, 2008 Author Share Posted March 2, 2008 What if I want to apply it to the whole page? Link to comment Share on other sites More sharing options...
boen_robot Posted March 2, 2008 Share Posted March 2, 2008 What if I want to apply it to the whole page?You apply it on the html element.In CSS:html {background: url(image.jpg)} and the equivalent in java script: document.documentElement.style.backgroundImage="url(image.jpg)"; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.