Dynamic settimeout with PHP

Asked

Viewed 188 times

0

I’m trying to get a certain time post update every 1 minute.

Code:

setTimeout(function() {     
   var ID = $(this).attr('id').split('hora')[1]; // pega o ID e retira a parte "like"
   $.get('index.php?hora=1&id='+ID, function(resultado){
   $('#hora'+ID).html(resultado);
});    
}, 2000 );

Error

index.php:502 Uncaught Typeerror: Cannot read Property split of Undefined(...)

  • how is the attr id coming?

  • <div class="hour" id="hour<? php echo $Row['idd']; ? >"> <? php $old date = $Row['datap']; echo time_elapsed(date($old date)); ? ></div>

  • 1

    Move $(this) for $('.hora'), does it work? I suspect that $(this) is not referencing the correct object.

  • Now there was no mistake, masss did not work :\

  • Help me @Ricardomoraleida

  • "did not work" is not a good error description @Viniciushenzel, remember that we only know about your problem what you wrote in the question. If you include more information, it makes it easier to help. Including an example of attr ID as it appears in the browser (i.e., after processing PHP) also helps.

  • sorry the way I spoke Ricardo, well now I realized posting two messages, one updated, the other not... strange... I’ll show you how it is, after processed: <div class="hora" id="hora196"> 3 minute ago</div>

  • People that strange, I realized that in the "source code" ta updated the time, but visibly not, ta stopped... Strange!!!

  • @Ricardomoraleida by the source code updates, and visible not

Show 4 more comments

1 answer

2


The this within setTimeout is not what you think. setTimeout runs in execution context window, therefore the this is not the element you want.

You have to refer to the element you want and then get the ID. Something like that:

var el = document.querySelector('#oMeuElemento');
setTimeout(function() {
    var id = el.id.split('hora')[1]; // pega o ID e retira a parte "like"
    $.get('index.php?hora=1&id=' + id, function(resultado) {
        $('#hora' + id).html(resultado);
    });
}, 2000);

If you want me to update every 1 minute, you have to change the 2000 (which is 2 seconds) and put 60000 (or 60 seconds x 1000 milliseconds)

  • Hi buddy, I tried but it only updates in the source code, visibly not, why will be ?

  • @Viniciushenzel the $.get is executed? What gives console.log(id, resultado); within that $.get?

  • Yes, so much so that it updates by source code, only visibly not.

  • @Viniciushenzel can answer what gives the console.log I asked on top?

  • How do I see it? By CTRL+SHIFT+N no error appears

  • Uncaught Typeerror: Cannot read Property 'id' of null(...)

  • @Viniciushenzel ok. And what you put as ID here -> var el = document.querySelector('#oMeuElemento');?

  • var el = Document.querySelector('. time');

  • @Viniciushenzel this element is not found, so the error. Is it there? or is it added later?

  • <div class="hour" id="hour<? php echo $Row['idd']; ? >"> <? php $old date = $Row['datap']; echo time_elapsed(date($old date)); ? ></div>

  • I’m so sad, God, it’s not like

  • @Viniciushenzel var el = document.querySelector('.hora'); gives null. when this code is run HTML is not there yet.

  • I put after the div and no longer gave the error, but only updates the source code, Aff

  • @Viniciushenzel is hard to guess what your code looks like. Create a jsFiddle with the problem and put it here. 2 minutes later is solved.

  • https://jsfiddle.net/th8hwhdr/

  • @Viniciushenzel what is supposed to bring the resultado?

  • @ViniciusHenzel ^ https://jsfiddle.net/5ppyr6s8/

  • IT WORKED VERY WELL var el = Document.querySelector('. time'); var time = 0; setInterval(Function() { var id = el.id.split('div')[1]; // takes the ID and removes the "like" $.get('index.php? hour=1&id=' +id, Function(result) { el.innerHTML = result + ' }); }, 500);

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.