main.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. jQuery.timer = function (interval, callback) {
  2. var interval = interval || 100;
  3. if (!callback)
  4. return false;
  5. _timer = function (interval, callback) {
  6. this.stop = function () {
  7. clearInterval(self.id);
  8. };
  9. this.internalCallback = function () {
  10. callback(self);
  11. };
  12. this.reset = function (val) {
  13. if (self.id)
  14. clearInterval(self.id);
  15. var val = val || 100;
  16. this.id = setInterval(this.internalCallback, val);
  17. };
  18. this.interval = interval;
  19. this.id = setInterval(this.internalCallback, this.interval);
  20. var self = this;
  21. };
  22. return new _timer(interval, callback);
  23. };
  24. // see: http://stackoverflow.com/questions/210717/using-jquery-to-center-a-div-on-the-screen
  25. jQuery.fn.center = function () {
  26. this.css("position","absolute");
  27. this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
  28. this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
  29. return this;
  30. }
  31. function log(str) {
  32. //firebug running
  33. if('object' == typeof console) {
  34. console.log(str);
  35. }
  36. }
  37. function simple_moving_averager(period) {
  38. var nums = [];
  39. return function(num) {
  40. nums.push(num);
  41. if (nums.length > period)
  42. nums.splice(0,1); // remove the first element of the array
  43. var sum = 0;
  44. for (var i in nums)
  45. sum += nums[i];
  46. var n = period;
  47. if (nums.length < period)
  48. n = nums.length;
  49. return(sum/n);
  50. }
  51. }