1
0

utils.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. function heriter(destination, source) {
  2. function initClassIfNecessary(obj) {
  3. if( typeof obj["_super"] == "undefined" ) {
  4. obj["_super"] = function() {
  5. var methodName = arguments[0];
  6. var parameters = arguments[1];
  7. this["__parent_methods"][methodName].apply(this, parameters);
  8. }
  9. }
  10. if( typeof obj["__parent_methods"] == "undefined" ) {
  11. obj["__parent_methods"] = {}
  12. }
  13. }
  14. for (var element in source) {
  15. if( typeof destination[element] != "undefined" ) {
  16. initClassIfNecessary(destination);
  17. destination["__parent_methods"][element] = source[element];
  18. } else {
  19. destination[element] = source[element];
  20. }
  21. }
  22. }
  23. /** PausableTimer **/
  24. function PausableTimer(func, millisec) {
  25. this.func = func;
  26. this.stTime = new Date().valueOf();
  27. this.timeout = setTimeout(func, millisec);
  28. this.timeLeft = millisec;
  29. }
  30. PausableTimer.prototype.stop = function() {
  31. clearTimeout(this.timeout);
  32. };
  33. PausableTimer.prototype.pause = function() {
  34. clearTimeout(this.timeout);
  35. var timeRan = new Date().valueOf()-this.stTime;
  36. this.timeLeft -= timeRan;
  37. };
  38. PausableTimer.prototype.resume = function() {
  39. this.timeout = setTimeout(this.func, this.timeLeft);
  40. this.stTime = new Date().valueOf();
  41. };
  42. //Usage:
  43. //var myTimer = new PausableTimer(function(){alert("It works!");}, 2000);
  44. //myTimer.pause();
  45. //myTimer.unpause();