Dynamic SetTimeOut with PHP

0

I'm trying to get an hour of a certain 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: Can not read property split of undefined (...)

    
asked by anonymous 06.11.2016 / 01:24

1 answer

2

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

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

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 to update every 1 minute, you have to change the 2000 (which is 2 seconds) and put 60000 (ie 60 seconds x 1000 milliseconds)

    
06.11.2016 / 09:41