Expand my Community achievements bar.

Help shape the future of AI assistance by participating in this quick card sorting activity. Your input will help create a more effective system that better serves your needs and those of your colleagues.
SOLVED

setInterval in a Condition?

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Level 8

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.

View solution in original post

3 Replies

Avatar

Correct answer by
Level 8

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.

Avatar

Level 2
Thanks, I like the second option that might be easier for us

Avatar

Level 10
Does the answer give you the info you need? If not, can you provide additional details to help the community better solve your question?