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?
 
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
}
0
 
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
0
 
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?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
newbiewebSr. Software EngineerAuthor Commented:
The data would contain on thing, the ID of the field that was updated.
0
 
newbiewebSr. Software EngineerAuthor Commented:
one thing, I meant
0
 
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.

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

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.