Computer

jQuery timer

// v.1 - 2021-02-18
jQuery.fn.timer = function (action, initial) {
	// Add "remove-timer-element" event
	if($.cleanData.toString().indexOf('"remove-timer-element"') == -1) {
		$.cleanData = (function ($cleanData) {
			return function(elements) {
				for(let i = 0; elements[i] != null; i++) {
					try {
						if(($._data(elements[i], "events") || {})["remove-timer-element"]) $(elements[i]).triggerHandler("remove-timer-element");
					} catch(e) {}
				}
				$cleanData(elements);
			};
		})($.cleanData);
	}
	// Get parameters
	switch(typeof(action)) {
		case "object": /* Ok. / Parameter "initial" is discarted. */ break;
		default      : options = { action: "start", initial: 0       }; break;
		case "string": options = { action: action , initial: initial }; break;
		case "number": options = { action: "start", initial: action  }; break;
	}
	// Validate parameters
	action  = ( typeof(options["action" ]) == "string" && ["start", "stop", "reset", "restart"].indexOf(options["action" ].toLowerCase()) != -1 ? options["action" ] : "start" );
	initial = ( typeof(options["initial"]) == "number" &&                                               options["initial"]                 >  0 ? options["initial"] : 0       );
	// Apply to each element
	return $(this).each(function () {
		let $this   = $(this);
		let show    = false  ;
		let actions = []     ;
		// Interval function
		let timer = function () {
			let s = $this.data("timer-time");
			$this
				.data("timer-time", s + 1)
				.text(() => { return [Math.floor(s / 3600), Math.floor(s / 60) % 60, s % 60].map(n => n < 10 ? "0" + n : n).join(":"); })
			;
		};
		// Setup call
		if(typeof($this.data("timer-interval")) == "undefined") {
			$this
				.data("timer-interval"      , null                                )
				.data("timer-time"          , initial                             )
				.on  ("remove-timer-element", function () { $this.timer("stop"); })
			;
			show = (action == "start" || action == "stop");
		}
		// Actions
		switch(action) {
			case "start"  : actions = [                 "start"]; break;
			case "stop"   : actions = ["stop"                  ]; break;
			case "reset"  : actions = ["stop", "reset"         ]; break;
			case "restart": actions = ["stop", "reset", "start"]; break;
		}
		for(let i in actions) {
			switch(actions[i]) {
				case "stop" : if($this.data("timer-interval") != null) { clearInterval($this.data("timer-interval"));
				                                                                       $this.data("timer-interval", null                    ); }            break;
				case "start": if($this.data("timer-interval") == null)                 $this.data("timer-interval", setInterval(timer, 1000));              break;
				case "reset":                                                          $this.data("timer-time"    , initial                 ); show = true; break;
			}
		}
		if(show) timer();
	});
};
USE IT AT YOUR OWN RISK!
Kudos: 0 [Give a kudo]

12r6j5xasef5Z6Sm4A6oBpMLn2ESPPX6ym
Ads: