moving setTimeout outside if/else statement