Function

Module: @ember/runloop
queue
String
The name of the queue to schedule against. Default queues is 'actions'.
target
Object
The target of the method to invoke.
method
Function|String
The method to invoke. If you pass a string it will be resolved on the target at the time the method is invoked.
args*
Object
Optional arguments to pass to the timeout.
returns
Object
Timer information for use in canceling, see `cancel`.

Schedules a function to run one time in a given queue of the current RunLoop. Calling this method with the same queue/target/method combination will have no effect (past the initial call).

Note that although you can pass optional arguments these will not be considered when looking for duplicates. New arguments will replace previous calls.

1
2
3
4
5
6
7
8
9
10
11
import { run, scheduleOnce } from '@ember/runloop';

function sayHi() {
  console.log('hi');
}

run(function() {
  scheduleOnce('afterRender', myContext, sayHi);
  scheduleOnce('afterRender', myContext, sayHi);
  // sayHi will only be executed once, in the afterRender queue of the RunLoop
});

Also note that for scheduleOnce to prevent additional calls, you need to pass the same function instance. The following case works as expected:

1
2
3
4
5
6
7
8
9
10
function log() {
  console.log('Logging only once');
}

function scheduleIt() {
  scheduleOnce('actions', myContext, log);
}

scheduleIt();
scheduleIt();

But this other case will schedule the function multiple times:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { scheduleOnce } from '@ember/runloop';

function scheduleIt() {
  scheduleOnce('actions', myContext, function() {
    console.log('Closure');
  });
}

scheduleIt();
scheduleIt();

// "Closure" will print twice, even though we're using `scheduleOnce`,
// because the function we pass to it won't match the
// previously scheduled operation.

Available queues, and their order, can be found at queues