The cycle time interval in setInterval is incorrect.

clipboard.png

what all the above JS code wants to achieve is that the text text changes every 2 seconds, but after each poll of the focusText array, that is, when text changes from "EXPLORE" to "TIME", the interval will exceed 2 seconds. Why?

Mar.13,2021

the array is out of bounds. It should be

.
iPP
if (i >= focusText.length) {
 i = 0
}

// i<focusTexti[0, 1, 2, ..., focus.length][0, 1, 2, ..., focus.length - 1]
if (i < focusText.length - 1) { 
 iPP
}

looked at the previous answer and seemed to miss the point.

=

The behavior of

setInterval is to queue the callback function at regular intervals. When the callback function can be executed depends on the previous code execution time.

ideally, if there is no executing code before, then the callback function can be executed immediately, but even then the time is not exactly 2 seconds, it is always a little more

.

this is a logic error. The scope of your I is [0, focusText.length] , not [0, focusText.length) . Feel that the code does not run as you want console.log , print debugging information is the most basic debug method.

Menu