Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setInterval() not working

I am trying to run a function at setInterval() of "1 second", but it is a bit problematic. I have done everything as shown here but it doesn't work.

Here is my code :

<script>
   function test(db_time)
   {
      var c_db_time= db_time/1000; 
      var current_time = new Date().getTime()/1000;
      return Math.round(current_time - c_db_time);
   }
   $(".elapsed_time").each(function() {
      var time_r = $(this).data('time_raw');
      var inter = $(this).html(time_ago(time_r));//parameter to function

      setInterval(inter,1000)
   });
</script>

And the error is :Uncaught SyntaxError: Unexpected identifier


Solution found thanks to @Bommox & @Satpal

 $(".elapsed_time").each(function() {
var time_r = $(this).data('time_raw');
    var self = $(this);
    var inter = function() {self.html(time_ago(time_r));}
    setInterval(inter, 1000);
  });
like image 588
Dev Man Avatar asked Sep 24 '26 11:09

Dev Man


2 Answers

into setInterval function's first parameter you should pass a function or an anonymous function like

setInterval(function(){
    console.log("1s delayed")
},1000);
like image 133
Roland Kákonyi Avatar answered Sep 26 '26 01:09

Roland Kákonyi


As said before, first argument should be the function:

 var self = $(this);
 var inter = function() {
     self.html(time_ago(time_r));
 }
 setInterval(inter, 1000);
like image 20
Jorgeblom Avatar answered Sep 25 '26 23:09

Jorgeblom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!