I was able to achieve this.
The problem was that the setInterval
was getting restarted (an observation mistake you can say) but it was getting bound immediately to an earlier element which was created dynamically hence new element which was getting added after certain period of time was not getting bound.
Also I forgot to mention that the method which was getting called i.e. outbound_reject_omniture_setting
was doing some work and then the parent of that element was getting hidden, so I was not able to check if the click is getting bound to the same element.
The solution was to loop through all the elements that are added till now and check if its parent is visible
. If yes then bound the click event to the element and clear the interval. Within the click function, restart the interval.
onInterval = function () {
$('.lpInviteChatHrefClose').each(function (index) {
if ($(this).parent().css('visibility') == 'visible') {
$(this).click(function () {
outbound_reject_omniture_setting(this);
interval = setInterval(onInterval, 1000);
});
clearInterval(interval);
}
})
};
interval = setInterval(onInterval, 1000);
Thank you everyone for your help.