nextTick
The usage method of this API is wx.nextTick(function callback).
Feature Description: Defers certain operations to be executed in the next time slice, similar to the setTimeout feature.
Parameter and Description: The function callback is used because the setData and triggerEvent interfaces in custom components are inherently synchronous operations. When these interfaces are called consecutively, they are executed within a single synchronous process, which may lead to errors in the case of improper logic.
An extreme case: If the parent component's setData triggers the child component's triggerEvent, causing the parent component to perform setData again, and in the process, the child component is unloaded through the wx:if statement, this could potentially cause an error. Therefore, for logic that does not need to be completed within a single synchronous process, this interface can be used to defer execution to the next time slice.
Sample Code
Component({
doSth() {
this.setData({ number: 1 })
wx.nextTick(() => {
this.setData({ number: 3 })
})
this.setData({ number: 2 })
}
})
Was this page helpful?