As said before, first argument should be the function:
var self = $(this);
var inter = function() {
self.html(time_ago(time_r));
}
setInterval(inter, 1000);
質問
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);
});
解決 2
As said before, first argument should be the function:
var self = $(this);
var inter = function() {
self.html(time_ago(time_r));
}
setInterval(inter, 1000);
他のヒント
into setInterval
function's first parameter you should pass a function or an anonymous function like
setInterval(function(){
console.log("1s delayed")
},1000);
var self = this;
setInterval(function(){
$(self).html(time_ago(time_r));
},1000);
Have a look here : window.setInterval
window.setTimeout( function() { /*your code which you want to execute after fixed interval, it can function name or lines of code*/}, /*fixedInterval*/);
window.setTimeout( function() { alert("Hello World!"); }, 5000); // it will execute after 5 seconds