[whatwg] 7.3 Timers
Alexandre Morgaut
Alexandre.Morgaut at 4d.com
Mon Feb 28 11:13:03 PST 2011
Here some Proposals / Features requests
Any comment would be welcome
http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html
1) arguments
I think that "args", the third parameter of setTimeout & setInterval is not enough described
Should arguments be an Array, or any "Array like" is ok (with indexed properties and a length property) ?
When I read: Any arguments are passed straight through to the handler.
I wonder if it does not mean that I should pass one parameter per argument to pass to the handler
-> setTimeout( myhandler, 1000, arg1, arg2, arg3);
But well, the signature looks like there is only one parameter so it must be at least an "Array like" object
Note that, in the process, there is steps to get the handler and the timeout parameters, but I didn't see any for arguments...
2) DateTime
A common use case with setTimeout is to compute the timeout argument from the current Date to process a function at a specific time
This can be easily done via a function like this one
function setToHappen(fn, d){
var t = d.getTime() - (new Date()).getTime();
return setTimeout(fn, t);
}
But couldn't setTimeout() accept natively a Date object in place of the timeout parameter
setTimeout(handle, datetime, arguments)
This could be implemented as a new method like setDateTimeout()
(it would make this feature easier to detect)
This would be useful for any application element which has:
- an expiration date,
- a refreshing date,
- a starting date
Examples:
- Tasks & Events in a Calendar
- Bids
- Promotions
- load of new stats the first day of the month at 4:00 AM
Nowadays, some Web Application stay in live in the browsers days, weeks or more before being reloaded
All the more, Timers are going to be also implemented on Server Side (as we did)
3) Durations
Sorry, this point may require minor updates to JavaScript core itself to be more consistent
Another useful feature would be to support more user friendly duration expressions like
setInterval(doItAtEachHour, "1 H");
setTimeout(logout, "20 mn");
Unfortunately the current Timers handle string a different way for the timeout parameter and this would generate bad results when not supported
I think what I would be more comfortable with is a Duration Class, It may be a little out of this scope but this may require setTimeout() and setInterval() to process it specifically if passed as timeout argument
Potential Interface
- its constructor may support either a local pattern or an ISO string as parameter
- its toString() method could generate a global user friendly string
- its toLocaleString() method could generate a local user friendly string
- its valueOf() method could return a number of millisecond
- it could have a toISOString() method
(Note that going this way, we may also ask for a Time class supporting the Time ISO format and supported by setTimeout )
ISO references
- http://en.wikipedia.org/wiki/ISO_8601#Durations
- http://dotat.at/tmp/ISO_8601-2004_E.pdf
Thoughts?
Alexandre.
More information about the whatwg
mailing list