2016-10-03 122 views
-3

我正在努力尝试使用第三方应用对网站的模式进行测试。我可以用这个工作的唯一方法是使用setInterval,但它无限期地运行.append。我已经尝试setTimeout没有效果,以及没有setInterval运行它。如果我可以设置一个计数器在计数到1后停止,我认为它会解决我的问题。以下是我到目前为止。另外,请注意,这些模式是从角度来说的,我不是很熟悉。如何让setInterval只运行一次?

$(document).ready(function() { 
    vwo_$(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); 
    }); 
+10

'我试过setTimeout没有效果'你在尝试setTimeout时面临什么问题? – gurvinder372

+2

'setInterval'只在一个名为'setTimeout'的特殊函数中运行一次。 –

回答

2

您可以检查元素的存在,当它存在取消间隔定时器

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

这也将让你减少间隔时间。