Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Forked from Noitidart/_template-bootstrapSkeleton.xpi
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noitidart/9088172 to your computer and use it in GitHub Desktop.
Save Noitidart/9088172 to your computer and use it in GitHub Desktop.
_demo-Bootstrap-nsITimer - Creates a timer that executes callback every 5s, waits for callback to finish, then starts again. Doing TYPE_REPEATING_SLACK with TYPE_ONE_SHOT. This is nice because if used TYPE_REPEATING_PRECISE will trigger this call back every myTimerInterval. TYPE_REPEATING_PRECISE_SKIP will trigger this call back every myTimerInt…
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
var myTimer = Cc['@mozilla.org/timer;1'].createInstance(Ci.nsITimer);
var myTimerCookie;
var myTimerInterval = 5000;
// we need an nsITimerCallback compatible interface for the callbacks.
var myTimerEvent = {
notify: function(timer) {
Cu.reportError('Timer Fired!');
//do stuff here, this stuff will finish and then timer will start countdown of myTimerInterval.
//This is nice because if used TYPE_REPEATING_PRECISE will trigger this call back every myTimerInterval. TYPE_REPEATING_PRECISE_SKIP will trigger this call back every myTimerInterval, but if myTimerInterval is up and the callback from last time myTimerInterval went off is still running, it will skip running this call back.
//TYPE_REPEATING_SLACK i don't trust because on MDN they said "note that this is not guaranteed: the timer can fire at any time." so I go with TYPE_ONE_SHOT.
timer.initWithCallback(myTimerEvent, myTimerInterval, Ci.nsITimer.TYPE_ONE_SHOT);
}
}
function startup(aData, aReason) {
//myTimer.initWithCallback(myTimerEvent, myTimerInterval, Ci.nsITimer.TYPE_ONE_SHOT);
//myTimerCookie = 'loadCount'; //just a global var you can set to pass some variable to the callback
myTimerEvent.notify(myTimer); //this is how we start the timer, we start off by running the callback, then from there every 5 sec it will call
//if want to start off by waiting 5sec first then comment out line 20 and uncomment line 18
}
function shutdown(aData, aReason) {
if (aReason == APP_SHUTDOWN) return;
myTimer.cancel();
}
function install() {}
function uninstall() {}
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
<em:id>Bootstrap-nsITimer@jetpack</em:id>
<em:version>initial</em:version>
<em:type>2</em:type>
<em:bootstrap>true</em:bootstrap>
<em:unpack>false</em:unpack>
<!-- Firefox -->
<em:targetApplication>
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>7.0</em:minVersion>
<em:maxVersion>27.0</em:maxVersion>
</Description>
</em:targetApplication>
<!-- Front End MetaData -->
<em:name>Bootstrap nsITimer</em:name>
<em:description>Bootstrap addon demonstrating a one shot nsITimer to create a repeating function. One shot means that the timer is not triggered again until the callback function is complete.</em:description>
<em:creator>Noitidart</em:creator>
</Description>
</RDF>
@Noitidart
Copy link
Author

README

@Noitidart
Copy link
Author

Example of setTimeout function with nsITimer:

function setTimeout(callback, timeout) {
    var timer = Cc['@mozilla.org/timer;'].createInstance(Ci.nsITimer);
    timer.initWithCallback(callback, timeout, Ci.nsITimer.TYPE_ONE_SHOT);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment