Have you looked at the API documentation?
In summary:
- getchar returns -1 when the window is closed, 0 if no key is pressed and a positive number otherwise.
- defer runs the given function on the next internal timer tick (EDIT: 30-50Hz). It also disables the undo point that running a script usually creates.