L10n.once

我们的志愿者还没有将这篇文章翻译为 中文 (简体)加入我们帮助完成翻译!
您也可以阅读此文章的English (US)版。

Non-standard
This feature is not on a current W3C standards track, but it is supported on the Firefox OS platform. Although implementations may change in the future and it is not supported widely across browsers, it is suitable for use in code dedicated to Firefox OS apps.

The once method is used to register a callback that will execute exactly once.  If the L10n context is ready when L10n.once() is called, the callback will be invoked immediately on the next tick of the event loop. If the L10n context is not ready when L10n.once() is called (because the localization resources are still downloading), the callback will be invoked when the ready event of the L10n context fires.

Using L10n.once() is safer than registering a self-deregistering listener of the window's localized event because it also works when the localized event has already fired. This helps prevent intermittent race conditions, which would be hard to debug.

Use L10n.once() to schedule initialization tasks for when the localization is ready to be used.

If you need to run some logic every time the language changes and/or when the L10n context first becomes ready (the 0th language change), use L10n.ready instead.

Syntax

navigator.mozL10n.once(callback);

Parameters

callback
The function to be executed once when the L10n context is or becomes ready.

Example

// mozL10n.once is the main entry point for the app.
navigator.mozL10n.once(function showBody() {
  // <body> children are hidden until the UI is translated
  document.body.classList.remove('hidden');
  // load frame_script.js for preview mode and show loading background
  if (!isPhone) {
    loader.load('js/frame_scripts.js');
  }
  // Now initialize the rest of the app.
  init();
});

Specification

Not part of any specification.

See also

文档标签和贡献者