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()?