Simplify javascript code

use event instead of an interval and passes jslint validation