cannot get delayed for loop working in javascript