HosipLan

Untitled

Feb 10th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @see http://stackoverflow.com/a/7356528
  3.  *
  4.  * @param functionToCheck
  5.  */
  6. function isFunction(functionToCheck) {
  7.     var getType = {};
  8.     return functionToCheck && getType.toString.call(functionToCheck) == '[object Function]';
  9. }
  10.  
  11.  
  12. /**
  13.  * @param arg
  14.  */
  15. function callback(arg) {
  16.     if (isFunction(arg)) {
  17.         return arg;
  18.     }
  19.  
  20.     return function () {
  21.         var obj = arg[0], method = arg[1];
  22.         obj[method]();
  23.     };
  24. }
  25.  
  26.  
  27.  
  28. /**
  29.  * Interval class
  30.  */
  31. var Interval = $class({
  32.     constructor: function (listener, time) {
  33.         this.callback = callback(listener);
  34.         this.time = time;
  35.         this.timeout = false;
  36.     },
  37.     start: function () {
  38.         var me = this;
  39.         this.timeout = setTimeout(function () {
  40.             me.callback();
  41.             me.start(); // next iteration
  42.         }, this.time);
  43.     },
  44.     pause: function () {
  45.         clearTimeout(this.timeout);
  46.         this.timeout = false;
  47.     }
  48. });
  49.  
  50.  
  51. var f = new Foo();
  52. var i = new Interval([f, 'm'], 1000);
  53. i.start();
Advertisement
Add Comment
Please, Sign In to add comment