Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-23-2011, 01:28 AM   PM User | #1
wisner94
New Coder

 
Join Date: Sep 2011
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
wisner94 is an unknown quantity at this point
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()?
wisner94 is offline   Reply With Quote
Old 09-23-2011, 05:34 AM   PM User | #2
morongo47
New Coder

 
Join Date: Sep 2011
Posts: 43
Thanks: 6
Thanked 6 Times in 6 Posts
morongo47 is an unknown quantity at this point
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.
morongo47 is offline   Reply With Quote
Old 09-23-2011, 09:26 AM   PM User | #3
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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);
			});
}
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
wisner94 (09-25-2011)
Old 09-25-2011, 08:00 PM   PM User | #4
wisner94
New Coder

 
Join Date: Sep 2011
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
wisner94 is an unknown quantity at this point
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.
wisner94 is offline   Reply With Quote
Old 09-25-2011, 08:02 PM   PM User | #5
wisner94
New Coder

 
Join Date: Sep 2011
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
wisner94 is an unknown quantity at this point
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.
wisner94 is offline   Reply With Quote
Old 09-28-2011, 11:09 PM   PM User | #6
wisner94
New Coder

 
Join Date: Sep 2011
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
wisner94 is an unknown quantity at this point
Tried the code. It works great! Thank y'all SO MUCH for helping me!
wisner94 is offline   Reply With Quote
Reply

Bookmarks

Tags
set, set timeout, settimeout, timeout, variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:44 AM.


Advertisement
Log in to turn off these ads.