User Tools

Site Tools


Single-Shot Timeout

Summary

Below is an example demonstrating how to create a single-shot timer that executes a function when that timer reaches a predefined duration.

API Areas of Interest

Example

Singleshot_Timeout.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	// Initialize 'static' variables that hold modifier key state
	var s_bShiftPressed = false;
	var s_bControlPressed = false;
	var s_bAltPressed = false;
	var s_bMetaPressed = false;
 
	// If the "Action" global transient is defined, and its the correct type
	if( typeof( Action ) != "undefined" && Action.inherits( "DzScriptAction" ) ){
		// If the current key sequence for the action is not pressed
		if( !App.isKeySequenceDown( Action.shortcut ) ){
			updateModifierKeyState();
		}
	// If the "Action" global transient is not defined
	} else if( typeof( Action ) == "undefined" ) {
		updateModifierKeyState();
	}
 
	/*********************************************************************/
	// String : A function for updating the keyboard modifier state
	function updateModifierKeyState()
	{
		// Get the current modifier key state
		var nModifierState = App.modifierKeyState();
		// Update variables that hold modifier key state
		s_bShiftPressed = (nModifierState & 0x02000000) != 0;
		s_bControlPressed = (nModifierState & 0x04000000) != 0;
		s_bAltPressed = (nModifierState & 0x08000000) != 0;
		s_bMetaPressed = (nModifierState & 0x10000000) != 0;
	};
 
	/*********************************************************************/
	// String : A function for printing only if debugging
	function debug()
	{
		// If we are not debugging
		if( !s_bAltPressed ){
			// We are done...
			return;
		}
 
		// Convert the arguments object into an array
		var aArguments = [].slice.call( arguments );
 
		// Print the array
		print( aArguments.join(" ") );
	};
 
	/*********************************************************************/
	// void : A function for handling when a timer times out
	// The 'this' object is expected to be a DzTimer object.
	function handleTimerTimeout(){
		try {
			debug( "Active =", this.active );
		} catch( sError ) {
			debug( sError );
		}
 
		this.deleteLater();
	};
 
	/*********************************************************************/
	// Create a new timer object
	var oTimer = new DzTimer();
 
	// Set the timer to be of the 'run once and stop' variety
	oTimer.singleShot = true;
 
	// Connect the timeout signal on the timer to a function we want to call;
	// an anonymous function in this case, wherein we report state and clean up
	oTimer.timeout.connect( oTimer, handleTimerTimeout );
 
	// Define timer duration, in milliseconds;
	// also used as a guard for max iterations in a 'busy' loop
	var nTimeout = 1000;
 
	// Start the timer
	oTimer.start( nTimeout );
 
	// Initialize a counter
	var nCount = 0;
 
	// While the timer is active, and we haven't exceeded our max
	while( oTimer.active && nCount <= nTimeout ){
		// Allow the application to keep working; !IMPORTANT!
		processEvents();
		// Report state and iteration
		debug( "oTimer.active =", oTimer.active, nCount );
		// Increment the count
		nCount += 1;
	}
 
// Finalize the function and invoke
})();