js loop bug fixed