Fix _generateEvent infinite loop problem

for line 957, should I use the toTime, endTime or clippedToTime(this require moving the block of code)?