Executing code

Methods to execute code based on a Later schedule.

Overview

The built-in implementations of setTimeout and setInterval only take a set number of milliseconds as the delay time. Later implements corresponding versions of these functions which can take advantage of Later schedules. This allows you to define flexible delays without having to compute them yourself.

setTimeout

Similar to the built-in implementation of setTimeout, later.setTimeout takes a function to execute and a delay, which in this case is defined by a Later schedule. The function specified will be executed exactly one time. The return value can be captured and used to clear the timer if desired.

  var t = later.setTimeout(function, schedule)
  t.clear();

Basic example

The following example will execute test 1 time.

  var sched = later.parse.recur().every(5).minute(),
      t = later.setTimeout(test, sched);

  function test() {
    console.log(new Date());
  }

Passing parameters

Parameters can be passed using an anonymous function call.

  var sched = later.parse.recur().every(5).minute(),
      t = later.setTimeout(function() { test(5); }, sched);

  function test(val) {
    console.log(new Date());
    console.log(val);
  }

setInterval

Similar to the built-in implementation of setInterval, later.setInterval takes a function to execute and a delay, which in this case is defined by a Later schedule. The function specified will be executed continually until cleared. The return value can be captured and used to clear the timer if desired.

  var t = later.setInterval(function, schedule)
  t.clear();

Basic example

The following example will execute test 5 times.

  var sched = later.parse.recur().every(5).minute(),
      t = later.setInterval(test, sched),
      count = 5;

  function test() {
    console.log(new Date());
    count--;
    if(count <= 0) {
      t.clear();
    }
  }

Passing parameters

Parameters can be passed using an anonymous function call.

  var sched = later.parse.recur().every(5).minute(),
      t = later.setInterval(function() { test(5); }, sched);

  function test(val) {
    console.log(new Date());
    console.log(val);
    t.clear();
  }