Skip to content

Custom Components

nextTick

TIP

The API usage is as follows: wx.nextTick(function callback)

  • Functional description: Delay some operations to the next time slice (similar to setTimeout).

  • Parameters and descriptions: function callback, because the setData and triggerEvent interfaces in the custom component are synchronous operations, when these interfaces are called continuously, they are all executed in a synchronous process, so improper logic may cause errors.

    An extreme case: when the setData of the parent component triggers the triggerEvent of the child component, which in turn causes the parent component to perform another setData, during which the child component is uninstalled through the wx:if statement, it may cause an error, so for logic that does not need to be completed in a synchronous process, this interface can be used to delay execution until the next time slice.

  • Sample code:

js
Component({
  doSth() {
    this.setData({ number: 1 }) // Executed directly within the current synchronous process

    wx.nextTick(() => {
      this.setData({ number: 3 }) // Executed after the current synchronous process ends, in the next time slice.
    })

    this.setData({ number: 2 }) // Executed directly within the current synchronous process
  }
})