Proposal: Switch position of delay and callback in signature of `setTimeout()`

Boris Zbarsky bzbarsky at mit.edu
Mon Apr 25 17:46:04 UTC 2016


On 4/25/16 1:35 PM, Adrian Sieber wrote:
> Everytime I use `setTimeout()`, I wonder why the the arguments are in
> the wrong order. Usually the callback functions comes last, but in
> 'setTimeout' it's exactly the other way round.

First, this is the wrong list: setTimeout is defined by 
https://html.spec.whatwg.org/multipage/webappapis.html#dom-settimeout 
not the ES language spec.

Second, note that the delay argument to setTimeout is optional.

> So a polyfill would be something like:
>
> ```
> newSetTimeout = (delay, callback, ...args) => {
>    if (typeof delay === 'function') {
>      setTimeout(delay, callback, ...args)
>    }
>    else {
>      setTimeout(callback, delay, ...args)
>    }
> }
> ```

Third, this is not compatible, obviously.  Consider what it would do with:

   setTimeout("doSomething()", 1000);

You could check for function or string, but even that is not compatible 
in cases like this:

   setTimeout({ toString: () => "doSomething()" }, 1000);

and then you're left discussing whether such cases exist in practice and 
whatnot, yes?

-Boris


More information about the es-discuss mailing list