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
// 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( function(){ print( "oTimer.active =", oTimer.active ); oTimer.deleteLater(); } );
 
// 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
	print( "oTimer.active =", oTimer.active, nCount );
	// Increment the count
	nCount += 1;
}