Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

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.
0
newbieweb
Asked:
newbieweb
  • 6
  • 4
1 Solution
 
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
 
newbiewebAuthor 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
 
newbiewebAuthor Commented:
The data would contain on thing, the ID of the field that was updated.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
newbiewebAuthor Commented:
one thing, I meant
0
 
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
 
newbiewebAuthor 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
 
newbiewebAuthor Commented:
Perfect! Thanks!
0
 
newbiewebAuthor 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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now