Added a condition in dispatchEvent.