Simplify javascript code

use events instead of an interval and passes jslint validation