How to implement a [timeout](https://en.wikipedia.org/wiki/Timeout_%28computing%29) in Javascript, not the `window.timeout` but something like `session timeout` or `socket timeout` - basically - a "`function timeout`"

> 
> A specified period of time that will be allowed to elapse in a system
>  before a specified event is to take place, unless another specified
>  event occurs first; in either case, the period is terminated when
>  either event takes place.
> 
> 
> 

Specifically, I want a javascript `observing timer` that will observe the execution time of a function and if reached or going more than a specified time then the `observing timer` will stop/notify the executing function.

Any help is greatly appreciated! Thanks a lot.