473,667 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delay the execution of a function with parameters

I have tried using setTimeout to delay the execution of a function with:

setTimeout('fun ction()', ms)

This works, but if the function has parameters:

setTimeout('fun ction(p1, p2, p3)', ms)

it does not work.

Any ideas very much appreciated.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Aug 12 '05 #1
5 11119
"Roger Withnell" wrote
I have tried using setTimeout to delay the execution of a function with:

setTimeout('fun ction()', ms)

This works, but if the function has parameters:

setTimeout('fun ction(p1, p2, p3)', ms)

it does not work.


You need to move the parameter variable names out of the string, so they are
interpreted rather than taken literally when the string is passed from the
timeout to the executioner:

setTimeout('fun ction(' + p1 + ', '+ p2 + ', ' + p3 + ')', ms)

hth
ivo
http://4umi.com/

Aug 12 '05 #2
simple.

setTimeout(func tion() { myFunc(a, b, c); }, ms);

enjoy.

Aug 12 '05 #3
ASM
Roger Withnell wrote:
I have tried using setTimeout to delay the execution of a function with:

setTimeout('fun ction()', ms)

This works, but if the function has parameters:

setTimeout('fun ction(p1, p2, p3)', ms)


setTimeout('fun ction('+p1+','+ p2+','+p3+')', ms)
--
Stephane Moriaux et son [moins] vieux Mac
Aug 12 '05 #4
"Other" <th**@that.so > writes:
"Roger Withnell" wrote
setTimeout('fun ction(p1, p2, p3)', ms)


Bad choice of function name, since "function" is a keyword in Javascript.
I'll call it "theFunctio n" instead.
You need to move the parameter variable names out of the string, so they are
interpreted rather than taken literally when the string is passed from the
timeout to the executioner:

setTimeout('fun ction(' + p1 + ', '+ p2 + ', ' + p3 + ')', ms)


That only works if the values converts to strings that are literals
representing the same value. That's generally not the case for
objects, nor for strings (i.e., it only works for numbers, booleans,
null and undefined). So, it's *not* a good idea in general.

Using variable names is better, but not much, since the variables need
to be in the scope where the string is evaluated, and that is the
global scope. So 'theFunction(p1 ,p2,p3)' *will* work, *if* p1, p2 and
p3 are the names of global variables.

The best alternative is to use a function as first argument to
setTimeout:
setTimeout(func tion(){theFunct ion(p1,p2,p3);} , ms);
It works in all modern browsers.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 13 '05 #5
ASM
Lasse Reichstein Nielsen wrote:

As I did give too this answer :
setTimeout('f unction(' + p1 + ', '+ p2 + ', ' + p3 + ')', ms)
That only works if the values converts to strings that are literals
representing the same value. That's generally not the case for
objects, nor for strings (i.e., it only works for numbers, booleans,
null and undefined). So, it's *not* a good idea in general.


I do think p1,2,3 are certainly names of variables
(this variables modified by main function before re-call to itself)
Using variable names is better, but not much, since the variables need
to be in the scope where the string is evaluated, and that is the
global scope. So 'theFunction(p1 ,p2,p3)' *will* work, *if* p1, p2 and
p3 are the names of global variables.
We can suppose they are not global
if not, I do not understand the why of question ...
The best alternative is to use a function as first argument to
setTimeout:
setTimeout(func tion(){theFunct ion(p1,p2,p3);} , ms);
that's the best way
It works in all modern browsers.


is that to say it will not work on my NC4.5 ?
--
Stephane Moriaux et son [moins] vieux Mac
Aug 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5874
by: Jason Shohet | last post by:
Lets say I want a button to execute 10 seconds after I press it, not immediately. Any ideas how to cause that, in a code-behind. IMO its only server side code, I don't think javascript needs to get in to the equation (?)
0
374
by: Max | last post by:
This is a follow-up on my previous thread concerning having the program wait for a certain date and time and then executing some code when it gets there. My question is; can I use the Sleep function from kernel32 to accomplish this? My concern is that this function takes milliseconds where my program needs to be accurate to within 5 minuets, and delays may be as long as a number of days, months or whatever. Would I run into many problems...
4
782
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I make a 10 second delay? ----------------------------------------------------------------------- There is no built-in way to pause execution in javascript such as a sleep function, but hosts usually provide a method of some form. Web browsers are designed for event driven programming and only provide the « setTimeout » and « setInterval »...
1
3655
by: klmishraa79 | last post by:
i want to know how i can put a time delay between two command execution...i.e. after first command of programm the second command should execute after some fixed delay......i want to use time delay other than sleep or timer....is it there any direct delay command like it is in c and c+...looking for ur suggestions.....thanks
1
7003
by: maul581 | last post by:
I know the delay( ) function of Turbo C and used it in my project. By this function we can give intermediate delay in execution from 1 millisecond to 9999 millisecond. My main question is "Is it possible to get delay less than 1 millisecond in Turbo C"? If by changin any header file (e.g. dos.h) or by creating my own "delay" function, at any way Can I get delay less than 1 millisecond? If in C it is not possible than please suggest me any...
0
8457
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8788
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8563
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8646
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6203
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.