Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

Need to use a Timer in JavaScript

I need my JavaScript to create a timer, then I need to code an event that fires when that timer is completed.

How do I code both the creation of the timer and the event?

Thanks.
Avatar of leakim971
leakim971
Flag of Guadeloupe image

setTimeout(function() {
      // set your event here
      // set your event here
      // set your event here
      // set your event here
      // set your event here
      // set your event here
      // set your event here
      // set your event here
}, 2000); // 2 seconds
Avatar of curiouswebster

ASKER

Cool.  How do I call it?

And can I add data to that event which I can later parse out in the case where I have two or more events firing?
The data would contain on thing, the ID of the field that was updated.
one thing, I meant
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, I do not need to attach data, if it's not possible. When I call setTimeout() the handle returned will be unique. True?

That function will continue to fire until I clear it. BUT, how do I know which instance is firing?

I will have many instances firing and need to be able to find which one it was that initiated it.

Perfect! Thanks!
One last question, how do I stop this from executing? I see that I can use a variable, thusly:

var t=setTimeout("javascript statement",milliseconds);

then t.clearTimeout().

But must I keep a list of my own, where I asociate an id with that handle returned by setTimeout()?