merge the codes from beforeDurationChange