metatron
12-13-2010, 09: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!
<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!