# Interstitial Ad

The interstitial ad component is composed of the client’s native image, text, and video controls. It has the highest level and will overwrite the general components.

Developers can call wx.createInterstitialAd to create an interstitial ad component. Each time the method is called, a brand new instance is returned. The instance is only valid for the current page, and is not allowed to be used across pages.

# Creation of Advertisement

The interstitial ad component is hidden by default. Therefore, it can be created in advance for the component to be initialized ahead of time. Developers can create advertisement instances in the onLoad event callback of the Mini Program page, and call the advertisement instances repeatedly throughout the lifecycle of the page.

let interstitialAd = null
Page({
  onLoad() {
    if(wx.createInterstitialAd){
      interstitialAd = wx.createInterstitialAd({ adUnitId: 'xxxx' })
      interstitialAd.onLoad(() => {
        console.log('onLoad event emit')
      })
      interstitialAd.onError((err) => {
        console.log('onError event emit', err)
      })
      interstitialAd.onClose((res) => {
        console.log('onClose event emit', res)
      })
    }
  }
})

# Show/Hide

The interstitial ad component is hidden by default and the developer needs to call InterstitialAd.show() to display it. If pulling an advertisement fails or triggers a frequency limit, the InterstitialAd.show() method will return a rejected Promise and the developer can listen error messages. Common unusual errors reference documentation

interstitialAd.show().catch((err) => {
  console.error(err)
})

Users can actively close interstitial ads. Developers cannot control the hiding of the interstitial ad components.

# Success and Failure of Advertisement Pulls

The interstitial ad component automatically pulls the ads and updates them. After the component is created, an ad will be pulled. After the user closes the ad, the next ad will be pulled.

If the pull is successful, the callback function registered via InterstitialAd.onLoad() will be executed, and the callback function has no parameter transmitted.

interstitialAd.onLoad(() => {
  console.log('Interstitial  ad loaded successfully')
})

If the pull fails, the callback function registered via InterstitialAd.onError() will be executed and the parameter of the callback function is an object containing the error message. Common unusual errors reference documentation

interstitialAd.onError(err => {
  console.log(err)
})

# Listen Users Closing Advertisements

If the ad is closed, the callback function registered via InterstitialAd.onClose() will be executed, and the callback function has no parameter passed.

interstitialAd.onClose(res => {
    console.log('The interstitial  ad is closed')
})

# Notes

Multiple calls to methods such as InterstitialAd.onLoad(), InterstitialAd.onError(), and InterstitialAd.onClose() to listen to an ad event will result in multiple event callbacks. It is recommended that you listen once after creating an ad, or cancel the original listening event before re-listening.