Timer : Timer

更新时间:
2024-03-13
下载文档

Timer : Timer

Timer is frequently used module, and JSRE supports the standard traditional JavaScript Timer (Timeout) and JSRE Timer classes.

Timer is the same as SigSlot, and uses the JSRE interrupt mechanism to notify events.

Example

  • Traditional timer
// Run the callback after one second only once.
setTimeout(() => {
  console.log('setTimeout() Timeout!');
}, 1000);

// Run the callback per second.
setInterval(() => {
  console.log('setInterval() Timeout!');
}, 1000);

// Run the callback when the system allows interrupts.
setImmediate(() => {
  console.log('setImmediate() Timeout!');
});
  • JSRE Timer
var t = new Timer();

// Run the callback after one second only once.
t.start(1000, () => {
  console.log('Timeout Timeout!');
});

// Run the callback per second.
t.start(1000, 1000, () => {
  console.log('Interval Timeout!');
});

// Run the callback when the system allows interrupts.
t.start(0, () => {
  console.log('Immediate Timeout!');
});

Support

The following shows Timer module APIs available for each permissions.

 User ModePrivilege Mode
Timer
timer.start
timer.stop
timer.remain
timer.isRepeat
timer.isStopped
timer.interval
timer.pause
timer.resume
timer.reset
timer.expire
setTimeout
setInterval
setImmediate
clearTimeout
clearInterval
clearImmediate

Timer Class

new Timer()

Create a JSRE timer object.

Example

var t = new Timer();

Timer Object

timer.start(count, func[, ...arg])

  • count {Integer} Timeout in milliseconds.
  • func {Function} Timer callback function.
  • ...arg {Any} Callback arguments. default: undefined.

Start the timer and wait for count milliseconds, then call func callback once. If the timer is running, restart the timer with new setting.

Example

function callback() {
  console.log('Timer Notify!');
}

var t = new Timer();

t.start(1000, callback);

timer.start(count, interval, func[, ...arg])

  • count {Integer} Timeout in milliseconds.
  • interval {Integer} Timer interval.
  • func {Function} Timer callback function.
  • ...arg {Any} Callback arguments. default: undefined.

Start the timer and wait for count milliseconds, then call func callback. then restart timer and call func per interval milliseconds. If the timer is running, restart the timer with new setting.

Example

var counter = 0;

function callback() {
  console.log('Timer Notify!', counter++);
}

var t = new Timer();

t.start(1000, 2000, callback);

timer.stop()

Stop timer. If the timer does not start, nothing happens.

timer.remain()

  • Returns: {Number} Remaining milliseconds.

How many milliseconds remaining from now to timeout. If the timer does not start, return positive infinity.

Example

var count = t.remain();
if (Number.isFinite(count)) {
  console.log('Timer remain:', count);
} else {
  console.log('Timer stoped!');
}

timer.isRepeat()

  • Returns: {Boolean} Is this timer a period timer.

Check timer type, The period timer returns true. otherwise false.

timer.isStopped()

  • Returns: {Boolean} Is this timer stopped.

Check if timer has stopped. This method is available on EdgerOS 1.10.1 and above.

timer.interval([interval])

  • interval {Integer} New interval.
  • Returns: {Integer} Previous interval.

Set or get a running timer interval. Calling this function must ensure that the timer is not in the stop state.

Example

var t = new Timer();

t.start(1000, 1000, function() {
  console.log('OK!');
  t.interval(3000); // Change interval to 3000ms
});

timer.pause()

  • Returns: {Boolean} Whether the operation was successful.

Pause a running timer.

timer.resume([timeout])

  • timeout {Integer} New timeouts (not interval).
  • Returns: {Boolean} Whether the operation was successful.

Resume a previously paused timer. If there is a timeout argument, the first timed time during resume is timeout, and if there is no timeout argument, the remaining time at the last pause() during resume is timed time.

timer.reset(count)

  • count {Integer} Timeout in milliseconds.
  • Returns: {Boolean} Whether the operation was successful.

When the timer is working, you can use this function to reset the current timing time of the timer. 0 means immediate timeout. This function will not change whether the timer is a periodic timer property. Invoke this method on a timer that is not running has no effect and returns false.

timer.expire()

  • Returns: {Boolean} Whether the operation was successful.

Causes a running timer to expire immediately. EdgerOS 1.7.1 and later versions support this feature.

Timeout Functions

The following API is a JavaScript traditional timer function.

setTimeout(callback, delay[, ...arg])

  • callback {Function} The function to call when the timer elapses.
  • delay {Integer} The number of milliseconds to wait before calling the callback.
  • ...args {Any} Optional arguments to pass when the callback is called. default: undefined.
  • Returns: {Timeout} for use with clearTimeout().

Schedules execution of a one-time callback after delay milliseconds.

Example

// Run the callback after one second only once.
setTimeout(() => {
  console.log('setTimeout() Timeout!');
}, 1000);

setInterval(callback, delay[, ...arg])

  • callback {Function} The function to call when the timer elapses.
  • delay {Integer} The number of milliseconds to wait before calling the callback.
  • ...args {Any} Optional arguments to pass when the callback is called. default: undefined.
  • Returns: {Timeout} for use with clearInterval().

Schedules repeated execution of callback every delay milliseconds.

Example

// Run the callback per second.
setInterval(() => {
  console.log('setInterval() Timeout!');
}, 1000);

setImmediate(callback[, ...arg])

  • callback {Function} Callback function.
  • ...args {Any} Optional arguments to pass when the callback is called. default: undefined.
  • Returns: {Timeout} for use with clearImmediate().

The timer interrupt is requested immediately. The timer callback function will be executed only when the current task is allowed to respond to the interrupt.

Example

// Run the callback when the system allows interrupts.
setImmediate(() => {
  console.log('setImmediate() Timeout!');
});

clearTimeout(timeout)

  • timeout {Timeout} A timeout object as returned by setTimeout().

Cancels a timeout object created by setTimeout().

clearInterval(timeout)

  • timeout {Timeout} A timeout object as returned by setInterval().

Cancels a timeout object created by setInterval().

clearImmediate(timeout)

  • timeout {Timeout} A timeout object as returned by setImmediate().

Cancels a timeout object created by setImmediate().

文档内容是否对您有所帮助?
有帮助
没帮助