# Insert Screen Advertising
The plug-in advertising component is made up of client-native image, text, and video controls, and is the highest level, covering the ordinary component.
Developers can call wx.createInterstitialAd to create an ad component.Each time this method is called, a new instance is returned, which is valid only for the current page and is not allowed to be used across pages.
# Advertising Creation
The plug-in ad component is hidden by default, so it can be created ahead of time to initialize the component in advance.Developers can create an ad instance in the onLoad event callback of the Weixin Mini Program page and repeatedly call the ad instance over the life 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 plug-in ad component is hidden by default, and developers need to call InterstitialAd.show () to display it.If the ad pull fails or trigger frequency limits, InterstitialAd.show () Method returns a rejectedPromise, and the developer can listen for error messages. Common Exceptions Error Reference Documentation
interstitialAd.show().catch((err) => {
console.error(err)
})
Users can actively turn off plug-in ads. Developers cannot control the hiding of plug-in ad components.
# Successes and failures of advertising campaigns
The plug-in ad component automatically pulls ads and updates them. An ad is pulled once after the component is created, and after the user closes the ad, the next ad is pulling.
If the pull is successful, use InterstitialAd.onLoad () The registered callback function is executed without parameter passing.
interstitialAd.onLoad(() => {
console.log('插屏 广告加载成功')
})
If the pull fails, use InterstitialAd.onError () A registered callback function will be executed, and the parameter of the callback function is an object that contains error information. Common Exceptions Error Reference Documentation
interstitialAd.onError(err => {
console.log(err)
})
# Listen to users turn off ads
If the advertisement is turned off, please contact the InterstitialAd.onClose () The registered callback function is executed without parameter passing.
interstitialAd.onClose(res => {
console.log('插屏 广告关闭')
})
# Note
Multiple calls InterstitialAd.onLoad() 、 InterstitialAd.onError() 、 InterstitialAd.onClose() Other methods of listening to an ad event generate multiple event callbacks. It is recommended to listen to the ad once after the ad is created, or to cancel the original listening event before relistening.
If you quickly switch pages during the display of a plug-in ad, a situation may occur when the plug-in is displayed on a non-invoked page. If necessary, display the plug-ins after the page switch is complete.