{"id":1986,"date":"2015-07-28T20:15:29","date_gmt":"2015-07-28T20:15:29","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1986"},"modified":"2015-07-28T20:16:34","modified_gmt":"2015-07-28T20:16:34","slug":"show-interstitial-ad-via-admob-in-ionic-every-2-minutes","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/show-interstitial-ad-via-admob-in-ionic-every-2-minutes\/","title":{"rendered":"Show Interstitial Ad via AdMob in Ionic every 2 minutes"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[1986]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>I actually answered <a href=\"http:\/\/stackoverflow.com\/questions\/31095303\/ionic-cordova-show-intersitial-ad-admob-every-2-minutes\/31098474\">this question<\/a>\u00a0by user<a href=\"http:\/\/stackoverflow.com\/users\/4921740\/arlind-a\">Arlind a<\/a>:<\/p>\n<p>I&#8217;m using AdMob plugin in Ionic and with this code I show an Interstital ad:<\/p>\n<pre class=\"lang:default decode:true \">function initAd(){\r\n\r\n \/\/ it will display smart banner at top center, using the default options\r\n if(AdMob) AdMob.createBanner( {\r\n                          adId: admobid.banner,\r\n                          bannerId: admobid.banner,\r\n                          position:AdMob.AD_POSITION.BOTTOM_CENTER,\r\n                          autoShow: true,\r\n                          isTesting: false,\r\n                          success: function(){\r\n                          console.log('banner created');\r\n                          },\r\n                          error: function(){\r\n                         console.log('failed to create banner');\r\n                          }\r\n                          } );\r\n\r\n                                       window.AdMob.prepareInterstitial( \r\n                           {adId:admobid.interstitial, autoShow:false} );\r\n    window.AdMob.showInterstitial();\r\n\r\n }<\/pre>\n<p>Is there a way to show intersitial ad every 2 minutes? Someone told me to add this:<\/p>\n<pre class=\"lang:default decode:true\">setInterval(showInterstitial,1*60*1000)<\/pre>\n<p>but I don&#8217;t know where to add?<\/p>\n<p>My <a href=\"http:\/\/www.stackoverflow.com\/questions\/31095303\/ionic-cordova-show-intersitial-ad-admob-every-2-minutes\/31098474#31098474\">answer<\/a> was:<\/p>\n<p>&nbsp;<\/p>\n<blockquote><p>If you would like to show it every 2 minutes you should use:<\/p>\n<pre class=\"lang:default decode:true\">setInterval(window.AdMob.showInterstitial, 2*60*1000);<\/pre>\n<p>and you should add it just before the closing bracket of your <code>initAdd<\/code> function:<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"lang:default decode:true \">function initAd(){\r\n\r\n\r\n \/\/ it will display smart banner at top center, using the default options\r\n if(AdMob) AdMob.createBanner( {\r\n                          adId: admobid.banner,\r\n                          bannerId: admobid.banner,\r\n                          position:AdMob.AD_POSITION.BOTTOM_CENTER,\r\n                          autoShow: true,\r\n                          isTesting: false,\r\n                          success: function(){\r\n                          console.log('banner created');\r\n                          },\r\n                          error: function(){\r\n                         console.log('failed to create banner');\r\n                          }\r\n                          } );\r\n\r\n                                       window.AdMob.prepareInterstitial( \r\n                           {adId:admobid.interstitial, autoShow:false} );\r\n    window.AdMob.showInterstitial();\r\n  \r\n  \r\n  \r\n  \/\/!!!add the code here!!! - so, just paste what I wrote above:\r\n  setInterval(window.AdMob.showInterstitial, 2*60*1000);\r\n\r\n }<\/pre>\n<p>You can see a simple setInterval usage on this <a href=\"http:\/\/jsfiddle.net\/np1cgx6a\/\" rel=\"nofollow\">jsFiddle example<\/a>:<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"lang:default decode:true \">function a(){\r\n    alert(\"hi every 2 seconds\");\r\n};\r\n\r\nsetInterval(a, 2*1000);<\/pre>\n<p>The reason why you shouldn&#8217;t call it like this (note the brackets after <code>a<\/code>): <code>setInterval(a(), 2*1000);<\/code> is that then your function would be called only once (you would see only one alert popping up). Example on <a href=\"http:\/\/jsfiddle.net\/uxt1Ljw1\/\" rel=\"nofollow\">jsFiddle<\/a>:<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"lang:default decode:true  \">function a(){\r\n    alert(\"hi every 2 seconds\");\r\n};\r\n\r\nsetInterval(a(), 2*1000);<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1991,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,35],"tags":[],"class_list":["post-1986","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ionic","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1986"}],"version-history":[{"count":1,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1986\/revisions"}],"predecessor-version":[{"id":1987,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1986\/revisions\/1987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/1991"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}