I would like to know IF and HOW it is possible to write a function which observes an other function.
More precisely, the obsever keeps track of the the observed function execution time, and in case the latter exceeds a given time value, the observer function stop it.
So, briefly:
function observed() { // ... }
/**
* Run a function and stop if this exceeds a given time.
*
* @param {Function} observedFunc The function to run and observe.
* @param {Number} maxTime The maximum elapsed time allowed.
*/
function observer(observedFunc, maxTime) {
/*
* if (observedFunc exceed the time)
* stop observedFunc;
* return;
* else
* return; (when the observed function return)
*/
}
Is it possible to afford this, without changing the observed function?
Only with Web Workers in browsers that support it. If you ran
observedfrom withing a Web Worker, then the main script could call terminate() on the worker.Normally (at least historically) Javascript is single threaded, and so while
observedis running,observerhas no way of stopping it.