0

I am trying to use set time function if a class exists in the document and that class has some specific data-attributes.So I have started to code in the very generic way and also tried all the ways even using setTimeout in the function, but not working...

Here is the code

jQuery(document).ready(function(jQuery){
    if( jQuery('.conversion-content').length > 0 ){
        var thread_id = jQuery('.conversion-content').attr('data-thread-id');
        var sender = jQuery('.conversion-content').attr('data-sender');
        setTimeout(function(){
            updateConversation(thread_id, sender);
        }, 2000);
    }
    function updateConversation( thread_id, sender ){
        console.log(thread_id,sender);
    }
});

Its working for the first time but not working from 2nd time, I had pulled out setTImeout function out of the element checking but no work.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Tick Twitch
  • 513
  • 10
  • 23

1 Answers1

0

First of all you should assign your thread_id and sender inside the timer, so it would change in time.

Second thing... it does shoot only once, because you are not using setInterval(), but setTimeout(). Only the first one is repeating in time, the second one delays the execution (once).

Third thing is that if at start if( jQuery('.conversion-content').length > 0 ){ won't be met, the timer won't even get initialized. So you should put this inside the timer as well.

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91