...

View Full Version : Using setInterval() improperly



metatron
12-13-2010, 10:08 AM
Hello. I'm trying to get a script (and an html page, I guess) to run constantly, and I'm pretty sure I should be using setInterval() to do this. But I'm not familiar enough with Javascript to know what I'm doing wrong, or what I need to fix. This is the code...


<html>
<head>
<script type="text/javascript" src="wz_jsgraphics.js"></script>
<script type="text/javascript">
<!--
function initialize_field() {
var jg = new jsGraphics();
var pixel_spacer = 10;
var field_length = 1000;
var field_width = 600;
jg.drawRect(pixel_spacer,pixel_spacer,field_length,field_width);
//...
return jg
}

function main() {
our_field = initialize_field();
dateobj = new Date();
current_time = dateobj.getMilliseconds();
our_field.drawString("Hello world!",current_time,500);
our_field.paint();
}


-->
</script>
</head>

<body onload="main(); setInterval('main()', 250 )">
</body>
</html>

If I understand setInterval() correctly, this should cause the page to run the main() function every 250 seconds. And within main(), the "Hello world!" string is being drawn at some point between 0 and 1000 pixels, depending upon the number of milliseconds, so it should essentially be scrolling across the screen. But I can only get it to "scroll" if I hammer F5 to repeatedly load the page. How can I get the main() function to run repeatedly?

I tried calling main() from main(), but that did not work well...

Thanks for any suggestions you have!

metatron
12-13-2010, 09:57 PM
Actually, now that I think about it, I don't want the main() function to just run repeatedly. Let's say that I wanted my little "Hello world!" string to move to a variety of places, one after the other.

How would I get my javascript to constantly update to display the string in a new place, and how would I get my javascript to measure the time that has passed since the script started running?

Rowsdower!
12-13-2010, 10:08 PM
I tracked down a copy of the graphics script that you're using and the problem seems to me that it uses document.write to populate your page. This effectively destroys everything that used to be in the page (your interval, for example). You need to update this function somethng like so(add the highlighted parts):


function pntDoc()
{
// this.wnd.document.write(jg_fast? this.htmRpc() : this.htm);
document.getElementsByTagName("body")[0].innerHTML=this.htm;
this.htm = '';
}

After that I had success with the timeouts and intervals that I tried to set.

Rowsdower!
12-13-2010, 10:15 PM
...How would I get my javascript to constantly update to display the string in a new place, and how would I get my javascript to measure the time that has passed since the script started running?

You could do this a few ways. You could just declare a variable and increment it each time you run main(), then calculate your increment mulitplied by your interval or timeout setting to get time elapsed. Or declare a start time variable and set it at the onload command. Then, with each running of main() check the current time against the start time and if "x" seconds have passed you exit the loop (or do whatever it is that depends on elapsed time).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum