Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Using setInterval() improperly

    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...

    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!

  • #2
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #3
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    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):

    Code:
    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.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    metatron (12-17-2010)

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by metatron View Post
    ...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).
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    metatron (12-17-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •