Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setTimeout pass arguments 1

Status
Not open for further replies.

skibascott

IS-IT--Management
Mar 18, 2004
82
US
Is it possible to pass arguments when using setTimeout? If so , what is the syntax?

Code:
setTimeout("ShellReader(openps)",5000);
 
Yes, use it as you would use any regular function, only in a string:

setTimeout("ShellReader(openps,1,2,3)",5000);
 
Maybe I don't quite understand your post, but "openps" is the variable that i want to pass.

Using this: setTimeout("ShellReader(openps)",5000);
returns an error saying that openps is undefined. When calling the function w\o the timeout, it works fine.
 
if opensps is a string then pass it in as
setTimeout("ShellReader('openps')",5000);
if it's a variable then
setTimeout("ShellReader(" + openps + ")",5000);
 
It is a variable.
When using: setTimeout("ShellReader(" + openps + ")",5000);
I get the error: Expected ')'
 
I can't think off the top of my head, but that should work as is. Try making a very basic script with just that function and a timer, and see what happens. If that works, then you have a different error.
 
Still nothing. Everything works the way it should until I put that variable into the call. Then I get the Expected ')' error.
Thanks for trying.
 
The problem is that when that line is parsed, the literal value for the string will be passed. For example:

I have this code:
var str = "hello, my name is kaht";

then I apply that to your code above:
setTimeout("ShellReader(" + str + ")",5000);

When the compiler parses that line it will get ready to call a function that looks like this:
ShellReader(hello, my name is kaht)

As you can see the quotes are missing around the string. So, to fix the problem, you simply need to put single quotes around the parameter passed, like so:
Code:
 setTimeout("ShellReader([COLOR=red][b]'[/b][/color]" + openps + "[COLOR=red][b]'[/b][/color])",5000);

Here's a working example you can paste into a new html file:
Code:
<script language="javascript">
var theString = "hello, my name is kaht";

function blah(str) {
   alert(str);
}

window.setTimeout("blah('" + theString + "')", 5000);
</script>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Ok. Thanks. That works, except that it tears the value of my variable to shreds.
Without the timeout, the openps variable passes a string like this:
/s /h /p \\server13a\apps\Process Sheets\tempsheets\8576.pdf
When using the timeout, the value is turned into this:
/s /h /p \server13aappsProcess Sheets empsheets8576.pdf

Is it possible to fix this, or do I just look for another way to set a timer?

 
That's just the nature of the backslash in javascript. It's treated as an escape character. \\ is the escape character for a single backslash (as you can see above right before "server13a" it changed it to a single backslash) \t is the escape character for a tab (as you can see above that "Sheets\tempsheets" got turned into "Sheets empsheets". The way to fix this would be to parse the string and double up all the backslashes so that they are all escaped and treated as literal backslashes. If I were to do this I would use a regexp so you could condense it to 1 line:
Code:
setTimeout("ShellReader('" + openps.replace(/\\/g, "\\\\") + "')",5000);
That's probably kinda hard to read and understand, but it should work.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top