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.
newbiewebSr. Software EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
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
newbiewebSr. Software EngineerAuthor Commented:
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?
newbiewebSr. Software EngineerAuthor Commented:
The data would contain on thing, the ID of the field that was updated.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

newbiewebSr. Software EngineerAuthor Commented:
one thing, I meant
leakim971PluritechnicianCommented:
Cool.  How do I call it?

it's a line of code, put it where you need it
 if you need to pass data you may use :


setTimeout("myfunction(some_id)", 2000); // 2 seconds

with :

function myfunction(id) {
      // 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
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
newbiewebSr. Software EngineerAuthor Commented:
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.

newbiewebSr. Software EngineerAuthor Commented:
Perfect! Thanks!
newbiewebSr. Software EngineerAuthor Commented:
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()?
leakim971PluritechnicianCommented:
http://www.w3schools.com/jsref/met_win_cleartimeout.asp
clearTimeout(t)

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.