setInterval in a Condition? | Community
Skip to main content
New Participant
September 11, 2020
Solved

setInterval in a Condition?

  • September 11, 2020
  • 2 replies
  • 1463 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Brian_Johnson_

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.

2 replies

jantzen_b
Employee
November 6, 2020
Does the answer give you the info you need? If not, can you provide additional details to help the community better solve your question?
Brian_Johnson_
Brian_Johnson_Accepted solution
New Participant
September 16, 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.

J2_2Author
New Participant
November 9, 2020
Thanks, I like the second option that might be easier for us