Monday, September 10, 2007

setTImer(func, 0) is useful on blur

In dynamic HTML we have a timer function.

Occasionally, you want to mimic table editor like one in the Microsoft SQL Management Studio. That is, you click on a row cell and edit the cells. A row is updated in the DB when row focus is lost.

In browser, 'focus' and 'blur' events are fired on input elements (text, radio, etc). The order of events is following: e1.blur, e2.focus when switching from e1 to e2. Now, we flush the changed row data into backstore DB unless e2 is an input element in the same row. The problem is that the target element e2 is not known at the blur time. So, we do not flush on the blur event -- we wait for the focus event. But what if focus never comes (in case of clicked on a non-input element)? Here setTimer(0) comes into play.

On blur, we set up a timer to fire immediately. We clear row 'isActive=false' flag on blur and set 'isActive=true' on focus. On the timer event we check the row isactive flag and flush if it is not anymore.

No comments: