setInterval in a Condition?

Avatar

Avatar
Boost 1
Level 1
J2_2
Level 1

Like

1 like

Total Posts

7 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Give Back
Validate 1
Shape 1
View profile

Avatar
Boost 1
Level 1
J2_2
Level 1

Like

1 like

Total Posts

7 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Give Back
Validate 1
Shape 1
View profile
J2_2
Level 1

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 (1)

Accepted Solutions (1)

Avatar

Avatar
Affirm 50
MVP
evolytics_brian
MVP

Likes

80 likes

Total Posts

170 posts

Correct reply

54 solutions
Top badges earned
Affirm 50
Applaud 25
Establish
Seeker
Give Back
View profile

Avatar
Affirm 50
MVP
evolytics_brian
MVP

Likes

80 likes

Total Posts

170 posts

Correct reply

54 solutions
Top badges earned
Affirm 50
Applaud 25
Establish
Seeker
Give Back
View profile
evolytics_brian
MVP

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.

Answers (0)