Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. function Timer(callback, delay) {
  2. var timerId, start, remaining = delay;
  3.  
  4. this.pause = function() {
  5. window.clearTimeout(timerId);
  6. remaining -= Date.now() - start;
  7. };
  8.  
  9. this.resume = function() {
  10. start = Date.now();
  11. window.clearTimeout(timerId);
  12. timerId = window.setTimeout(callback, remaining);
  13. };
  14.  
  15. this.resume();
  16. }
  17.  
  18. var timer = new Timer(function() {
  19. alert("Done!");
  20. }, 1000);
  21.  
  22. timer.pause();
  23. // Do some stuff...
  24. timer.resume();
  25.  
  26. function Timer(fn, countdown) {
  27. var ident, complete = false;
  28.  
  29. function _time_diff(date1, date2) {
  30. return date2 ? date2 - date1 : new Date().getTime() - date1;
  31. }
  32.  
  33. function cancel() {
  34. clearTimeout(ident);
  35. }
  36.  
  37. function pause() {
  38. clearTimeout(ident);
  39. total_time_run = _time_diff(start_time);
  40. complete = total_time_run >= countdown;
  41. }
  42.  
  43. function resume() {
  44. ident = complete ? -1 : setTimeout(fn, countdown - total_time_run);
  45. }
  46.  
  47. var start_time = new Date().getTime();
  48. ident = setTimeout(fn, countdown);
  49.  
  50. return { cancel: cancel, pause: pause, resume: resume };
  51. }
  52.  
  53. function Timer(callback, delay) {
  54. var args = arguments,
  55. self = this,
  56. timer, start;
  57.  
  58. this.clear = function () {
  59. clearTimeout(timer);
  60. };
  61.  
  62. this.pause = function () {
  63. this.clear();
  64. delay -= new Date() - start;
  65. };
  66.  
  67. this.resume = function () {
  68. start = new Date();
  69. timer = setTimeout(function () {
  70. callback.apply(self, Array.prototype.slice.call(args, 2, args.length));
  71. }, delay);
  72. };
  73.  
  74. this.resume();
  75. }
  76.  
  77. function callback(foo, bar) {
  78. console.log(foo); // "foo"
  79. console.log(bar); // "bar"
  80. }
  81.  
  82. var timer = new Timer(callback, 1000, "foo", "bar");
  83.  
  84. timer.pause();
  85. document.onclick = timer.resume;
  86.  
  87. // Setting
  88. var t = setInterval(doSomething, 1000);
  89.  
  90. // Pausing (which is really stopping)
  91. clearInterval(t);
  92. t = 0;
  93.  
  94. // Resuming (which is really just setting again)
  95. t = setInterval(doSomething, 1000);
  96.  
  97. var pt_hey = new PauseableTimeout(function(){
  98. alert("hello");
  99. }, 2000);
  100.  
  101. window.setTimeout(function(){
  102. pt_hey.pause();
  103. }, 1000);
  104.  
  105. window.setTimeout("pt_hey.start()", 2000);
  106.  
  107. var pi_hey = new PauseableInterval(function(){
  108. console.log("hello world");
  109. }, 2000);
  110.  
  111. window.setTimeout("pi_hey.pause()", 5000);
  112.  
  113. window.setTimeout("pi_hey.resume()", 6000);
  114.  
  115. 'use strict';
  116.  
  117.  
  118. //Constructor
  119. var Timer = function(cb, delay) {
  120. this.cb = cb;
  121. this.delay = delay;
  122. this.elapsed = 0;
  123. this.remaining = this.delay - self.elapsed;
  124. };
  125.  
  126. console.log(Timer);
  127.  
  128. Timer.prototype = function() {
  129. var _start = function(x, y) {
  130. var self = this;
  131. if (self.elapsed < self.delay) {
  132. clearInterval(self.interval);
  133. self.interval = setInterval(function() {
  134. self.elapsed += 50;
  135. self.remaining = self.delay - self.elapsed;
  136. console.log('elapsed: ' + self.elapsed,
  137. 'remaining: ' + self.remaining,
  138. 'delay: ' + self.delay);
  139. if (self.elapsed >= self.delay) {
  140. clearInterval(self.interval);
  141. self.cb();
  142. }
  143. }, 50);
  144. }
  145. },
  146. _pause = function() {
  147. var self = this;
  148. clearInterval(self.interval);
  149. },
  150. _restart = function() {
  151. var self = this;
  152. self.elapsed = 0;
  153. console.log(self);
  154. clearInterval(self.interval);
  155. self.start();
  156. };
  157.  
  158. //public member definitions
  159. return {
  160. start: _start,
  161. pause: _pause,
  162. restart: _restart
  163. };
  164. }();
  165.  
  166.  
  167. // - - - - - - - - how to use this class
  168.  
  169. var restartBtn = document.getElementById('restart');
  170. var pauseBtn = document.getElementById('pause');
  171. var startBtn = document.getElementById('start');
  172.  
  173. var timer = new Timer(function() {
  174. console.log('Done!');
  175. }, 2000);
  176.  
  177. restartBtn.addEventListener('click', function(e) {
  178. timer.restart();
  179. });
  180. pauseBtn.addEventListener('click', function(e) {
  181. timer.pause();
  182. });
  183. startBtn.addEventListener('click', function(e) {
  184. timer.start();
  185. });
  186.  
  187. <button onclick="myBool = true" > pauseTimeout </button>
  188.  
  189. <script>
  190. var myBool = false;
  191.  
  192. var t = setTimeout(function() {if (!mybool) {dosomething()}}, 5000);
  193. </script>
  194.  
  195. var Slideshow = {
  196.  
  197. _create: function(){
  198. this.timer = window.setInterval(function(){
  199. $(window).trigger('timer:tick'); }, 8000);
  200. },
  201.  
  202. play: function(){
  203. $(window).bind('timer:tick', function(){
  204. // stuff
  205. });
  206. },
  207.  
  208. pause: function(){
  209. $(window).unbind('timer:tick');
  210. }
  211.  
  212. };
  213.  
  214. function Timer( callback, delay ) {
  215.  
  216. /** Get access to this object by value **/
  217. var self = this;
  218.  
  219.  
  220.  
  221. /********************* PROPERTIES *********************/
  222. this.delay = delay;
  223. this.callback = callback;
  224. this.starttime;// = ;
  225. this.timerID = null;
  226.  
  227.  
  228. /********************* METHODS *********************/
  229.  
  230. /**
  231. * Pause
  232. */
  233. this.pause = function() {
  234. /** If the timer has already been paused, return **/
  235. if ( self.timerID == null ) {
  236. console.log( 'Timer has been paused already.' );
  237. return;
  238. }
  239.  
  240. /** Pause the timer **/
  241. window.clearTimeout( self.timerID );
  242. self.timerID = null; // this is how we keep track of the timer having beem cleared
  243.  
  244. /** Calculate the new delay for when we'll resume **/
  245. self.delay = self.starttime + self.delay - new Date().getTime();
  246. console.log( 'Paused the timer. Time left:', self.delay );
  247. }
  248.  
  249.  
  250. /**
  251. * Resume
  252. */
  253. this.resume = function() {
  254. self.starttime = new Date().getTime();
  255. self.timerID = window.setTimeout( self.callback, self.delay );
  256. console.log( 'Resuming the timer. Time left:', self.delay );
  257. }
  258.  
  259.  
  260. /********************* CONSTRUCTOR METHOD *********************/
  261.  
  262. /**
  263. * Private constructor
  264. * Not a language construct.
  265. * Mind var to keep the function private and () to execute it right away.
  266. */
  267. var __construct = function() {
  268. self.starttime = new Date().getTime();
  269. self.timerID = window.setTimeout( self.callback, self.delay )
  270. }(); /* END __construct */
  271.  
  272. } /* END Timer */
  273.  
  274. var timer = new Timer( function(){ console.log( 'hey! this is a timer!' ); }, 10000 );
  275. timer.pause();
  276.  
  277. <div id="div1">1</div><div id="div2">2</div>
  278. <div id="div3">3</div><div id="div4">4</div>
  279. <script>
  280. function hideDiv(elm){
  281. var interval,
  282. unit = 1000,
  283. cycle = 5,
  284. hide = function(){
  285. interval = setInterval(function(){
  286. if(--cycle === 0){
  287. elm.style.display = 'none';
  288. clearInterval(interval);
  289. }
  290. elm.setAttribute('data-cycle', cycle);
  291. elm.innerHTML += '*';
  292. }, unit);
  293. };
  294. elm.onmouseover = function(){
  295. clearInterval(interval);
  296. };
  297. elm.onmouseout = function(){
  298. hide();
  299. };
  300. hide();
  301. }
  302. function hideDivs(ids){
  303. var id;
  304. while(id = ids.pop()){
  305. hideDiv(document.getElementById(id));
  306. }
  307. }
  308. hideDivs(['div1','div2','div3','div4']);
  309. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement