mozilla

Revision 348911 of DeferredTask.jsm

  • Revision slug: Mozilla/JavaScript_code_modules/DeferredTask.jsm
  • Revision title: DeferredTask.jsm
  • Revision id: 348911
  • Created:
  • Creator: mnoorenberghe
  • Is current revision? No
  • Comment

Revision Content

{{ gecko_minversion_header("18.0") }}

The {{ Source("toolkit/content/DeferredTask.jsm", "DeferredTask.jsm") }} JavaScript code module offers utility routines for a task that will run after a delay. Multiple attempts to run the same task before the delay will be coalesced. To use it, you first need to import the code module into your JavaScript scope:

Components.utils.import("resource://gre/modules/DeferredTask.jsm");

Use this for instance if you write data to a file and you expect that you may have to rewrite data very soon afterwards. With DeferredTask, the task is delayed by a few milliseconds and, should a new change to the data occur during this period,

  • only the final version of the data is actually written;
  • a further grace delay is added to take into account other changes.

{{ h1_gecko_minversion("The DeferredTask constructor", "18.0") }}

If you have a function call you want to defer for 2 seconds, you can do so using the DeferredTask constructor, like this:

var task = new DeferredTask(myFunction, 2000);

Method overview

bool isPending();
void start();
void flush();
void cancel();

Methods

isPending()

Check the current task state.

bool isPending();
Return value

Returns true if pending, false otherwise.

start()

Start (or postpone) task.

void start();

flush()

Perform any postponed task immediately.

void flush();

cancel()

Cancel any pending task.

void cancel();

Revision Source

<p>{{ gecko_minversion_header("18.0") }}</p>
<p>The <code>{{ Source("toolkit/content/DeferredTask.jsm", "DeferredTask.jsm") }}</code> JavaScript code module offers utility routines for a task that will run after a delay. Multiple attempts to run the same task before the delay will be coalesced. To use it, you first need to import the code module into your JavaScript scope:</p>
<pre>
Components.utils.import("resource://gre/modules/DeferredTask.jsm");
</pre>
<p>Use this for instance if you write data to a file and you expect that you may have to rewrite data very soon afterwards. With <code>DeferredTask</code>, the task is delayed by a few milliseconds and, should a new change to the data occur during this period,</p>
<ul>
  <li>only the final version of the data is actually written;</li>
  <li>a further grace delay is added to take into account other changes.</li>
</ul>
<p>{{ h1_gecko_minversion("The DeferredTask constructor", "18.0") }}</p>
<p>If you have a function call you want to defer for 2 seconds, you can do so using the <code>DeferredTask</code> constructor, like this:</p>
<pre>
var task = new DeferredTask(myFunction, 2000);
</pre>
<h2 id="Method_overview" name="Method_overview">Method overview</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <td><code>bool <a href="#isPending()">isPending</a>();</code></td>
    </tr>
    <tr>
      <td><code>void <a href="#start()">start</a>();</code></td>
    </tr>
    <tr>
      <td><code>void <a href="#flush()">flush</a>();</code></td>
    </tr>
    <tr>
      <td><code>void <a href="#cancel()">cancel</a>();</code></td>
    </tr>
  </tbody>
</table>
<h2 id="Methods" name="Methods">Methods</h2>
<h3 id="isPending()" name="isPending()">isPending()</h3>
<p>Check the current task state.</p>
<pre class="eval">
bool isPending();
</pre>
<h6 id="Return_value" name="Return_value">Return value</h6>
<p>Returns true if pending, false otherwise.</p>

<h3 id="start()" name="start()">start()</h3>
<p>Start (or postpone) task.</p>
<pre class="eval">
void start();
</pre>

<h3 id="flush()" name="flush()">flush()</h3>
<p>Perform any postponed task immediately.</p>
<pre class="eval">
void flush();
</pre>

<h3 id="cancel()" name="cancel()">cancel()</h3>
<p>Cancel any pending task.</p>
<pre class="eval">
void cancel();
</pre>
Revert to this revision