我在Ionic中使用AdMob插件,并使用此代码显示了一个Interstital广告:
function initAd(){ // it will display smart banner at top center,using the default options if(AdMob) AdMob.createBanner( { adId: admobid.banner,bannerId: admobid.banner,position: AdMob.AD_POSITION.BOTTOM_CENTER,autoShow: true,isTesting: false,success: function() { console.log('banner created'); },error: function() { console.log('failed to create banner'); } }); window.AdMob.prepareInterstitial({ adId:admobid.interstitial,autoShow:false }); window.AdMob.showInterstitial(); }
有没有办法每2分钟显示一个部分广告?有人告诉我添加这个:setInterval(showInterstitial,1 * 60 * 1000),但我不知道在哪里添加?
解决方法
如果您想每2分钟显示一次,您应该使用:
setInterval(window.AdMob.showInterstitial,2*60*1000);
你应该在initAdd函数的右括号之前添加它:
function initAd(){ // it will display smart banner at top center,using the default options if(AdMob) AdMob.createBanner( { adId: admobid.banner,position:AdMob.AD_POSITION.BOTTOM_CENTER,success: function(){ console.log('banner created'); },error: function(){ console.log('failed to create banner'); } } ); window.AdMob.prepareInterstitial( {adId:admobid.interstitial,autoShow:false} ); window.AdMob.showInterstitial(); //!!!add the code here!!! - so,just paste what I wrote above: setInterval(window.AdMob.showInterstitial,2*60*1000); }
您可以在此jsFiddle example上看到一个简单的setInterval用法:
function a(){ alert("hi every 2 seconds"); }; setInterval(a,2*1000);
你之所以不应该这样称呼它(注意a之后的括号):setInterval(a(),2 * 1000);那么你的函数只会被调用一次(你会看到只弹出一个警告). jsFiddle的例子:
function a(){ alert("hi every 2 seconds"); }; setInterval(a(),2*1000);
希望这有助于清除一些事情.