fixed typo, added immediate
updated example
Increased performance with setImmediate, moved flag clear to end of the dispatch loop