setInterval()不起作用
我试图在setInterval()
“1秒”运行一个函数,但它有点问题。 我已完成如此处所示的所有操作但不起作用。
这是我的代码:
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) });
错误是: Uncaught SyntaxError:意外的标识符
感谢@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); });
如前所述,第一个参数应该是函数:
var self = $(this); var inter = function() { self.html(time_ago(time_r)); } setInterval(inter, 1000);
进入setInterval
函数的第一个参数你应该传递一个函数或一个匿名函数
setInterval(function(){ console.log("1s delayed") },1000);
var self = this; setInterval(function(){ $(self).html(time_ago(time_r)); },1000);
看看这里: 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