Function

scheduleOnce (queue, target, method, args*) Object public

Module: @ember/runloop
import { scheduleOnce } from '@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.

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:

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:

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

© 2020 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember/3.25/functions/@ember%2Frunloop/scheduleOnce