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 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question setTimeout() with variable?

    I'm writing a JS that will update the source of a picture on a webpage. The problem is, users must be able to adjust the interval at which the img src is updated. Users type a framerate into a textbox with a button next to it which calls the function changeFrameRate():

    Code:
    	function changeFrameRate()
    {
    	fps = parseFloat(document.getElementById('frameRateBox').value)
    	Timer = 1/fps*1000
    	if(Timer < 200)
    		{
    		alert("The framerate is too fast!")
    		}
    	if(Timer >= 200)
    		{
    		frameRate = Timer
    		}
    }
    *NOTE: frameRate is a global variable.\

    Next, I submit the request to update img src to the server with function updateImage():

    Code:
    function updateImage()
    {
    	request = new Object();
    	request.scale = [.7,.7];
    	request.cmd='update_camera_frame';
    	jsondata = JSON.stringify(request);
    	$.post(address, jsondata, function(parsedData)
    			{
    		var d = new Date();
    		document.getElementById('picture').src = baseURL + "/" + parsedData.path + "?" + d.getTime();
    			});
    	setTimeout("updateImage()",200);
    }
    Currently, the update interval is 200 ms. The code works fine. But I want to be able to use variable frameRate as the update interval, and no matter how many ways I write the setTimeout() method, the entire function updateImage() does not work. I've tried setTimeout("updateImage()",frameRate) and setTimeout("updateImage()","frameRate"), but neither works.

    Does anyone know how I can use a variable to set the timeout interval with setTimeout()?

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    43
    Thanks
    6
    Thanked 6 Times in 6 Posts
    Where/when do you initialize frameRate? Is it like: var frameRate=200; ?

    You might try actually printing-out frameRate from another function just to see if it really has global visibility, since that's what everything hinges on.

    Globals are attached to the window object, so window.frameRate should be visible.

    Try changing it's value in your form and see if a dummy function can 'see' it and print it out.

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    You'll use the variable as setTimeout(..., frameRate) ... but in combination with an Ajax request it will not work this way because of the asynchronous nature of asynchronous requests :-)

    Move the setTimeout() into the callback function and it will be a lot better already

    Second hint: Forget about strings as the first parameter of setTimeout. They are syntactically correct but they pose a performance and security problem. Better use a function reference or an anonymous function as the first parameter
    Code:
    function updateImage()
    {
    	request = new Object();
    	request.scale = [.7,.7];
    	request.cmd='update_camera_frame';
    	jsondata = JSON.stringify(request);
    	$.post(address, jsondata, function(parsedData)
    			{
    		var d = new Date();
    		document.getElementById('picture').src = baseURL + "/" + parsedData.path + "?" + d.getTime();
    		setTimeout(updateImage, frameRate);
    			});
    }

  • Users who have thanked devnull69 for this post:

    wisner94 (09-25-2011)

  • #4
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by morongo47 View Post
    Where/when do you initialize frameRate? Is it like: var frameRate=200; ?

    You might try actually printing-out frameRate from another function just to see if it really has global visibility, since that's what everything hinges on.

    Globals are attached to the window object, so window.frameRate should be visible.

    Try changing it's value in your form and see if a dummy function can 'see' it and print it out.
    Already'd done that. frameRate works fine.

  • #5
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    You'll use the variable as setTimeout(..., frameRate) ... but in combination with an Ajax request it will not work this way because of the asynchronous nature of asynchronous requests :-)

    Move the setTimeout() into the callback function and it will be a lot better already

    Second hint: Forget about strings as the first parameter of setTimeout. They are syntactically correct but they pose a performance and security problem. Better use a function reference or an anonymous function as the first parameter
    Code:
    function updateImage()
    {
    	request = new Object();
    	request.scale = [.7,.7];
    	request.cmd='update_camera_frame';
    	jsondata = JSON.stringify(request);
    	$.post(address, jsondata, function(parsedData)
    			{
    		var d = new Date();
    		document.getElementById('picture').src = baseURL + "/" + parsedData.path + "?" + d.getTime();
    		setTimeout(updateImage, frameRate);
    			});
    }
    Thanks!!! I'm going to try this. I'll let you know how it comes out.

  • #6
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Tried the code. It works great! Thank y'all SO MUCH for helping me!


  •  

    Tags for this Thread

    Posting Permissions

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