home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / firefox / alarm.js next >
Text File  |  2006-08-07  |  2KB  |  53 lines

  1. function G_Alarm(callback, delayMS, opt_repeating, opt_maxTimes) {
  2. this.debugZone = "alarm";
  3. this.callback_ = callback;
  4. this.repeating_ = !!opt_repeating;
  5. var Cc = Components.classes;
  6. var Ci = Components.interfaces;
  7. this.timer_ = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  8. var type = opt_repeating ?
  9. this.timer_.TYPE_REPEATING_SLACK :
  10. this.timer_.TYPE_ONE_SHOT;
  11. this.maxTimes_ = opt_maxTimes ? opt_maxTimes : null;
  12. this.nTimes_ = 0;
  13. this.timer_.initWithCallback(this, delayMS, type);
  14. }
  15. G_Alarm.prototype.cancel = function() {
  16. if (this.timer_) {
  17. this.timer_.cancel();
  18. this.timer_ = null;
  19. this.callback_ = null;
  20. }
  21. }
  22. G_Alarm.prototype.notify = function(timer) {
  23. var ret = this.callback_();
  24. this.nTimes_++;
  25. if (this.repeating_ &&
  26. typeof this.maxTimes_ == "number"
  27. && this.nTimes_ >= this.maxTimes_) {
  28. this.cancel();
  29. } else if (!this.repeating_) {
  30. this.cancel();
  31. }
  32. return ret;
  33. }
  34. G_Alarm.prototype.QueryInterface = function(iid) {
  35. if (iid.equals(Components.interfaces.nsISupports) ||
  36. iid.equals(Components.interfaces.nsIObserver) ||
  37. iid.equals(Components.interfaces.nsITimerCallback))
  38. return this;
  39. throw Components.results.NS_ERROR_NO_INTERFACE;
  40. }
  41. function G_ConditionalAlarm(callback, delayMS, opt_repeating, opt_maxTimes) {
  42. G_Alarm.call(this, callback, delayMS, opt_repeating, opt_maxTimes);
  43. this.debugZone = "conditionalalarm";
  44. }
  45. G_ConditionalAlarm.inherits(G_Alarm);
  46. G_ConditionalAlarm.prototype.notify = function(timer) {
  47. var rv = G_Alarm.prototype.notify.call(this, timer);
  48. if (this.repeating_ && rv) {
  49. G_Debug(this, "Callback of a repeating alarm returned true; cancelling.");
  50. this.cancel();
  51. }
  52. }
  53.