DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Luis has posted 1 posts at DZone. View Full User Profile

Pause Function For JavaScript

01.15.2008
| 1359 views |
  • submit to reddit
        http://ourworld.cs.com/tcmits1/javascript/papers/pause/pause.html

<script type = "text/javascript"language = "JavaScript">
 
/*	constructor 
	duration integer seconds
	<optional> function to run while waiting.
*/
function Pause(duration, busy) {
	this.duration = duration * 1000;
	this.busywork = null;
    // function to call while waiting.
    this.runner = 0;

    if (arguments.length == 2) {
        this.busywork = busy;

    }
    this.pause(this.duration);
}/* Pause class*/

/* pause method 
      duration: integer in seconds 
*/
  
Pause.prototype.pause = function(duration) {
    if ((duration == null) || (duration < 0)) {
        return;
    }

    var later = (new Date()).getTime() + duration;

    while (true) {
        if ((new Date()).getTime() > later) {
            break;

        }
        this.runner++;

        if (this.busywork != null) {
            this.busywork(this.runner);
        }
    }/* while*/
}/*pause method*/

 </script>