SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    May 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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
    PHP Code:
    <!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
    PHP Code:
    function UpdateUserOnline(user_idlogin_key){
        
        $.
    post("onlinecheck.php", {iduser_idkeylogin_key } );
        
        
    setTimeout(UpdateUserOnline(user_idlogin_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.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,895
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    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:

    Code javascript:
    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.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Member
    Join Date
    May 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Member
    Join Date
    May 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  5. #5
    SitePoint Member
    Join Date
    May 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it working anyway. Very strange but this is what I had to do:

    PHP Code:
    setTimeout("UpdateUserOnline(user_id, login_key )"60000); 
    So basically I just had to put double quotes around the UpdateUserOnline() function in the setTimeout() function.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,603
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ncwdavid View Post
    PHP Code:
    setTimeout("UpdateUserOnline(user_id, login_key )"60000); 
    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:

    PHP Code:
    setTimeout(function() { UpdateUserOnline(user_idlogin_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()
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Member
    Join Date
    May 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright cool thanks for that never knew about the eval() thing. It's all working perfectly now.


Bookmarks

Posting Permissions

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