setTimeout() updates over 100 times a second but i set its delay to 1 minute

Hey I am writing this Javascript function that runs every 1 minute using the setTimeout() function. Basically it is ment to run every 1 minute and post a user id and a unique key to a PHP script and then the PHP script updates the database. This is the code:

online.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title here</title>
<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
<script type="text/javascript" src="online.js"></script>
</head>
<body onLoad="UpdateUserOnline(8, 'c89017bf298df1048faac7abce9291ac' )">
</body>
</html>

online.js


function UpdateUserOnline(user_id, login_key){
	
	$.post("onlinecheck.php", {id: user_id, key: login_key } );
	
	t = setTimeout(UpdateUserOnline(user_id, login_key ), 60000);

}

The onlinecheck.php at the moment only writes to a file the user_id and the login_key but when I run the online.php file and it loads the UpdateUserOnline() function the browser just freezes and when I view the file that the data is getting written to the size of it is just flying up by the second and it has thousands of lines in it.

Ive tried looking everywhere and I can’t seem to find a solution. I still don’t see how it is running the function over 100 times a second when I told it to run every 60 seconds which im fairly sure is 60000 miliseconds. Any help would do. Thanks.

It’s executing the function instead of scheduling it for later so the function ends up in an infinite loop. You’d need something like this instead:


function UpdateUserOnline(user_id, login_key){
    $.post("onlinecheck.php", {id: user_id, key: login_key } );
   
    t = setTimeout(function() { UpdateUserOnline(user_id, login_key ) }, 60000);
} 

That way the setTimeout is set to the anonymous function, and that function will be called when 60 seconds are over.

Thanks for the fast reply and is that okay to do? As in just leave a blank function() before the real function? I was looking at examples of clocks and they all had like 500 milisecond delays and repeating back on the same function and none of them froze up the browser. Really confuses me.

I just did what you said above and there is a difference. The difference is the browser does not freeze up, the page fully loads and the browser is responsive but it still keeps looping through at the same speed or maybe even faster and writing to the file.

I still don’t get it. Should it not just load and then with the setTimeout function not load the function again for another 60000 miliseconds?

I got it working anyway. Very strange but this is what I had to do:


t = setTimeout("UpdateUserOnline(user_id, login_key )", 60000);

So basically I just had to put double quotes around the UpdateUserOnline() function in the setTimeout() function.

When you pass in a string JavaScript does an eval() on it in order to get the code to run.

A more efficient way is to pass it a function directly. You can’t put the () on the end of the function though or it will run straight away so when you need that you wrap it in an anonymous function:

t = setTimeout(function() { UpdateUserOnline(user_id, login_key ) }, 60000);

which is what JavaScript has to convert the string into anyway in order to run it.

If the anonymous function version didn’t work it would be because you typed it wrong.

In place of the first " put function() { and in place of the second " put } and it will get rid of the internally run eval()

Alright cool thanks for that never knew about the eval() thing. It’s all working perfectly now.