# Screen advertising
The insert ad component is composed of client-side native picture, text, and video controls, which are the highest level and will be overlaid on ordinary components.
Developers can call wx.createInterstitialAd Create a plug-in ad component. Each time the method is called, a whole new instance is returned,This instance is valid only for the current page and is not allowed across pages.
# Advertising creation
The screen ads component is hidden by default, so it can be created in advance to initialize the component in advance. The developer can view the Mini Program page on the onLoad Event callback and invoke the ad instance repeatedly 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 emitting, err)
})
interstitialAd.onClose((res) => {
console.log('onClose event emitting, res)
})
}
}
})
# display/hide
The plug-in ad component is hidden by default, and the developer needs to call InterstitialAd.show() For display. If the ad pull fails or triggers the frequency limit,InterstitialAd.show() The method returns a rejected Promise, developers can listen for error messages themselves.Common Exception Errors Reference Document
interstitialAd.show().catch((err) => {
console.error(err)
})
Users can actively turn off advertising. The developer cannot control the hiding of the ad component.
# Advertising Success and Failure
The display ad component automatically pulls and updates ads. An ad is pulled once after the component is created, and the user closes the ad and pulls down the next ad.
If the pull is successful, pass the InterstitialAd.onLoad() The registered callback function is executed without parameter passing.
interstitialAd.onLoad(() => {
console.log('Put Ad Loaded Successfully ')
})
If the pull fails, pass the InterstitialAd.onError() The registered callback function is executed with an object containing an error message as its argument.Common Exception Errors Reference Document
interstitialAd.onError(err => {
console.log(err)
})
# Listening to users turn off ads
If the ad is turned off, by InterstitialAd.onClose() The registered callback function is executed without parameter passing.
interstitialAd.onClose(res => {
console.log('Put Advertisingclosed')
})
# Note
Multiple calls InterstitialAd.onLoad()、InterstitialAd.onError()、InterstitialAd.onClose() Methods such as monitoring ad events will produce multiple event callbacks, it is recommended to monitor once after creating an ad, or cancel the original monitoring event and then re-monitor.
If you quickly switch pages during the process of screen placement, it may appear that screen placement ads are displayed on non-calling pages. If necessary, please display screen placement ads after the completion of page switching.