Javascript timeout function which guarantees the proper order of exeuction