setInterval in a Condition?

J2_2

11-09-2020

Hey All,

 

I sometimes need to tell my rules to wait for a certain object to pop into existence (either a JS object or something of that nature).  I can normally do this using setInterval in a Rule Event, and then the rule fires when needed Yay!

 

Can I do something similar in a Condition, but say with DOM Loaded as the Rule Event?  I can't seem to get it to work right - I'm not a dev by trade so if I'm using something basic incorrectly here feel free to yell at me:

 

var IntervalIterator = 0;

var MyInterval = setInterval(
function(){
var DataLayer = window.pageData;
if(typeof DataLayer !== "undefined" && DataLayer !== null && DataLayer !==""){
clearInterval(MyInterval);
return true  //Tell the condition that it is true now
}else if (IntervalIterator == 5){ //ensure we don't have a runaway function
clearInterval(MyInterval);
return false // The condition fails because object never loaded
}else{
IntervalIterator++;
}
}, 1000); //end setInterval

Accepted Solutions (0)

Answers (1)

Answers (1)

evolytics_brian

16-09-2020

Hi, @J2_2 -

There are a ton of options here, and they all depend on your site setup and preference. One option I'll offer would be to create TWO rules as follows:

 

Rule 1: Direct call rule that does whatever it is you need to do when the element/object you're waiting for is available

Rule 2: Page load rule that runs everywhere. The only action in this rule is to poll for the element/object. When/if it exists, it calls the DCR (rule 1 above).

 

Another approach might be to create a data element that points to a specific key on your data layer, then set the rule so that it fires on data element change.