Why PIJS command sleep(5) waits full 5 sec and always returns false ? I mean, why impossible interrupt sleep ?
The sleep function is not part of the ECMA standard. It is a custom utility implemented in Mozilla's JavaScript shell
js (which is actually a testing/demonstration tool for the SpiderMonkey engine).
PJSR's sleep function is a method of the Global object and has the following signature:
void sleep( Number seconds )This method implements a non-interruptible wait state. Internally it has been implemented as a call to
Sleep() on Windows and to
usleep() on Linux/UNIX platforms.
To implement an interruptible wait, you must write a waiting loop yourself. For example:
#define WAIT_GRANULARITY 0.05 // in seconds
function MyDialog()
{
// ...
this.abortWait = false;
this.wait = function( waitMaxSecs )
{
this.abortWait = false;
var waitMaxSteps = Math.round( waitMaxSecs/WAIT_GRANULARITY );
for ( var count = 0; count < waitMaxSteps && !this.abortWait; ++count )
{
sleep( WAIT_GRANULARITY ); // wait
processEvents(); // process all pending GUI events
}
return !this.abortWait; // returns true if the wait was not interrupted
};
// ...
this.abort_Button = new PushButton( this );
this.abort_Button.text = "Abort";
this.abort_Button.onClick = function()
{
this.abortWait = true;
};
// ...
this.wait( 10 );
}
A future version of PixInsight (perhaps 1.6.1) will implement a new Timer object in PJSR. Timer will work either as a one-shot or as a continuous programmable timer with an onTimeout event handler that will be called at each timer interval.