Time events in Javascript

From Yate Documentation
(Difference between revisions)
Jump to: navigation, search
(Created page with "===Timer and Timeouts in JavaScript=== The Engine class from JavaScript has 4 methods to process and cancel timeouts: NOTE: The time interval values are in milliseconds. * ...")
 
(Example)
 
(12 intermediate revisions by one user not shown)
Line 1: Line 1:
 +
Timers in Javascript are used to callback a method at a certain time interval. Timeout method creates an one shoot time event.
 +
 +
You can use this methods instead of using [[engine.timer]] message.
 +
 +
This events are used to make an action in a certain period of time.
 +
 
===Timer and Timeouts in JavaScript===
 
===Timer and Timeouts in JavaScript===
  
 
The Engine class from JavaScript has 4 methods to process and cancel timeouts:
 
The Engine class from JavaScript has 4 methods to process and cancel timeouts:
  
NOTE: The time interval values are in milliseconds.
+
'''Note''': The time interval values are in milliseconds.
  
* Engine.setInterval(callback,expireInterval)
+
* '''Engine.setInterval(callback,expireInterval)'''
  
 
This method will create a repeated time event. Each time expireInterval passes, callback method is called.
 
This method will create a repeated time event. Each time expireInterval passes, callback method is called.
  
This method returns an id witch can be used to cancel the timer with the help of Engine.clearInterval method.
+
This method returns an id which can be used to cancel the timer with the help of Engine.clearInterval method.
  
* Engine.clearInterval(id)
+
* '''Engine.clearInterval(id)'''
  
 
This method clears a time event installed with Engine.setInterval method.
 
This method clears a time event installed with Engine.setInterval method.
Line 19: Line 25:
 
The method returns true if the time event was cleared.
 
The method returns true if the time event was cleared.
  
* Engine.setTimeout(callback,expireInterval)
+
* '''Engine.setTimeout(callback,expireInterval)'''
  
This method crates an one shoot time event. After the expireInterval has passed the callback method is called and callback method is called; after this the time event is cleared.
+
This method creates an one shoot time event. After the expireInterval has passed the callback method is called; after this the time event is cleared.
  
This method returns an id, witch can be used to clear the timeout event.
+
This method returns an id, which can be used to clear the timeout event.
  
* Engine.clearTimeout(id)
+
* '''Engine.clearTimeout(id)'''
  
 
This method clears a time event installed with Engine.setTimeout method.
 
This method clears a time event installed with Engine.setTimeout method.
  
 +
===Example===
  
Examples:
+
In the example below the method ''intervalCallback'' will be called each second.
 +
 
 +
The ''timeoutCallback'' method will be called after 10 seconds and will clear the interval timeout. So after 10 seconds no message will be shown.
  
 
  var intervalId;
 
  var intervalId;
Line 46: Line 55:
 
  intervalId = Engine.setInterval(intervalCallback,1000);
 
  intervalId = Engine.setInterval(intervalCallback,1000);
  
In the example above the method intervalCallback will be called each second. The timeoutCallback method will be called after 10 seconds and it will clear the interval timeout. So after 10 seconds no message will be shown.
+
 
 +
 
 +
'''See also'''
 +
 
 +
* [[Javascript]]
 +
 
 +
[[Category:Javascript]] [[Category:Programmers]]

Latest revision as of 16:32, 31 October 2013

Timers in Javascript are used to callback a method at a certain time interval. Timeout method creates an one shoot time event.

You can use this methods instead of using engine.timer message.

This events are used to make an action in a certain period of time.

[edit] Timer and Timeouts in JavaScript

The Engine class from JavaScript has 4 methods to process and cancel timeouts:

Note: The time interval values are in milliseconds.
  • Engine.setInterval(callback,expireInterval)

This method will create a repeated time event. Each time expireInterval passes, callback method is called.

This method returns an id which can be used to cancel the timer with the help of Engine.clearInterval method.

  • Engine.clearInterval(id)

This method clears a time event installed with Engine.setInterval method.

The id parameter represents the value returned by Engine.setInterval method.

The method returns true if the time event was cleared.

  • Engine.setTimeout(callback,expireInterval)

This method creates an one shoot time event. After the expireInterval has passed the callback method is called; after this the time event is cleared.

This method returns an id, which can be used to clear the timeout event.

  • Engine.clearTimeout(id)

This method clears a time event installed with Engine.setTimeout method.

[edit] Example

In the example below the method intervalCallback will be called each second.

The timeoutCallback method will be called after 10 seconds and will clear the interval timeout. So after 10 seconds no message will be shown.

var intervalId;

function timeoutCallback() {
    Engine.debug(Engine.DebugAll,"Got timeout event!");
    Engine.clearInterval(intervalId);
}

function intervalCallback() {
    Engine.debug(Engine.DebugAll,"Got interval event!");
}

Engine.setTimeout(timeoutCallback,10000);
intervalId = Engine.setInterval(intervalCallback,1000);


See also

Personal tools
Namespaces

Variants
Actions
Preface
Configuration
Administrators
Developers