Skip to content Skip to sidebar Skip to footer

Settimeout Usage Issues

I have the following JavaScript and I am having issues within using setTimeout, ie: function setUrl() { // Get system URL details var url_path = new String(window.location.path

Solution 1:

Don't use a string as the first argument of the setTimeout function call, use an anonymous function.

You also had an extra curly brace:

functionsetUrl() {
  // Get system URL detailsvar url_path = window.location.pathname,
      new_url = window.location.host;

  setTimeout(function () {
    setValue('ONE_UP_URL',new_url);
  }, 2000);
}

If you use a string, it will evaluated and that is not really recommended.

Evaluating code by using eval (and its relatives, Function, setTimeout, and setInterval) is considered dangerous, because they will execute the code you pass with the privileges of the caller, and almost all the times there is a workaround to avoid them.

Other minor things:

  • The call to the String constructor in your code is redundant, since window.location.pathname is already a string.
  • You can declare your function variables on a single var statement.

Solution 2:

You have a rogue closing brace. Either there is more code missing, or you just need to remove it. (The line above setTimeout.)

Also, you should replace this:

setTimeout("setValue('ONE_UP_URL',new_url);",2000);

with this:

setTimeout(function() { setValue('ONE_UP_URL', new_url); }, 2000);

Solution 3:

Try updating it to something like:

functionsetValue( s, variable ) {
    alert( s );
    alert( variable );
}

functionsetUrl()
{
  // Get system URL detailsvar url_path = window.location.pathname, 
      new_url = window.location.host;

  setTimeout(
    function() {
        setValue('ONE_UP_URL',new_url);
    }, 2000 );
}

Post a Comment for "Settimeout Usage Issues"