JB Everett JB Everett - 4 months ago 31
jQuery Question

How do I get setInterval to only run once?

I'm working on trying to run a test on modals for a site with a 3rd party app. The only way I can get this to work is with setInterval, but it runs my .append indefinitely. I've tried setTimeout to no effect, as well as running it without setInterval. If I could just set a counter to stop after counting to 1, I think it would solve my problems. Below is what I have so far. Also, please note that these modals are running off of angular, something I'm not very familiar with.

$(document).ready(function() {
setInterval(function() {
vwo_$(".reveal-modal").append("<hr> <h3> Text \"pz330\" to Shell1 and redeem your Pennzoil® oil change coupon today. </h3> <br> <p> <em>Message and data rates may apply. For more details, please see our <a href=\"http://www.shell.com/privacy.html\" target=\"_blank\">Privacy Policy</a> and <a href=\"http://www.shell.com/terms-of-use.html\" target=\"_blank\">Terms and Conditions</a>. </em> </p> <hr> <a data-ng-click=\"redeemOffer(\'clickPrintCTA\');\" href=\"redeem?print=true\" class=\"button radius expand desktopCTABtn\" data-equalizer-watch=\"\"> <div><i class=\"fa fa-print fa-lg\"></i>&nbsp;&nbsp;<span>PRINT COUPONS</span></div> </a></div>");
}, 500);


You can check for existence of the element and when it exists cancel the interval timer

var timer = setInterval(function() {
   if(vwo_$(".reveal-modal").length ){
}, 200);

This will also allow you do decrease the interval duration.