#1
  1. I lov C in AIX/Linux, hate C++
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jul 2003
    Location
    Jacksonville, Florida
    Posts
    1,655
    Rep Power
    34

    JS Error - Useless setTimeout call ??


    I created a script and tested it out. Instead I get this javascript error in SeaMonkey/Firefox...

    --snip--
    Error: useless setTimeout call (missing quotes around argument?)
    --snip--

    So, what did I do wrong??

    Code:
    <script type="text/javascript">
    function WebTimerTimeOut(TypeOfUser)
    {
    	/* Reference: http://demarzo.net/archive/2005/01/30/324.aspx           */
       /* Reference: http://www.howtocreate.co.uk/tutorials/javascript/timers */
    	/* Note, most browsers have a minimum delay length of between 25 and 75 ms. If a      */
       /* shorter delay is specified, the actual delay will be the minimum delay length.     */
       /* Even with higher numbers, the delay is never perfect. Most browsers will take      */
       /* slightly longer than the time you ask for, typically just a few miliseconds error. */
    
       window.clearTimeout();  /* To clear out remaining timer from previous web page [Just in case] or with browser refresh button... */
    
    	var location;
    
    	if (TypeOfUser == "ADMIN") {
    		var minutes = 60;    
    	} else if (TypeOfUser == "CUST") {
    		var minutes = 15;    
    	}
    
    	var browserTimeOutTimer = (minutes * 60 * 1000);  /* # of minute(s) * 60 seconds per minute x 1,000 milliseconds per second */
    	var location = "https://www.domain.com/exit.php?exit_status=EXPIRED&wp=2&debug_timer="+minutes;
       window.setTimeout(window.location.replace(location),browserTimeOutTimer);
    }
    
    window.onload = function () {
       WebTimerTimeOut('CUST');
    }
    </script>
    Thanks....
  2. #2
  3. c0der
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2005
    Location
    Vancouver
    Posts
    665
    Rep Power
    155
    location.replace(location) will be executed right away. Void is returned, and the setTimeout call becomes useless.

    setTimeout is looking for either a function reference or a string to evaluate. It could look like this:

    Code:
    // This is where the "missing quotes" complaint is hinting at
    setTimeout("window.location.replace(location);", browserTimeOutTimer);
    Or like this....

    Code:
    setTimeout(function() { window.location.replace(location); }, browserTimeOutTimer);
    And in the interest of variety, it could also look like this:

    Code:
    setTimeout(new Function("window.location.replace(location);"), browserTimeOutTimer);
    By the way, I haven't thoroughly read your code, but I did notice a misused clearTimeout. clearTimeout accepts a single argument, and that's supposed to be a setTimeout id. No timeouts are carried around between page reloads. Even if there was one, clearTimeout(null) wouldn't stop it.

    Edit: Alright, I spotted another error. This is why I laugh at people who are overly verbose. "window.location" is no different than "location"... It's just more finger work. People who spell everything out never seem to get that. You are overriding the upper level location variable, which means location.href will change a string's property, but not actually do anything special; i.e. redirect the user in x seconds. Call your custom "location" variable "l", or "newPage", or... Pretty much anything, really. Just don't call it location.

    window.anything is the same as anything. Anything is the same as window['Anything']. Get the picture? eval("j" + i + " = 32"); is the same as window["j" + i] = 32;, albeit the former is uglier by tenfold.
    Last edited by Joseph Taylor; June 5th, 2006 at 12:06 PM.
  4. #3
  5. I lov C in AIX/Linux, hate C++
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jul 2003
    Location
    Jacksonville, Florida
    Posts
    1,655
    Rep Power
    34
    Interesting... It just have to have a string to work, or a use defined function instead... thanks...

IMN logo majestic logo threadwatch logo seochat tools logo