setInterval( fnTime(function(hour,minute,second){ console.log(hour+":"+minute+":"+second); }), 1000 );
function fnTime(callback){ var date = new Date(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); callback(hour,minute,second); }
这样貌似是不行的,小弟初学请大侠们指点,谢谢
5 回复
setInterval( fnTime(function(hour,minute,second){ console.log(hour+":"+minute+":"+second); }), 1000 ); setInterval执行字符串或一个回调函数,这里的fnTime是执行调用,会把fnTime的返回结果放在setInterval当成字符串运行,由于fnTime没有return,所以返回的是undefined 要是在function fnTime(callback){ }最后面加上return 'console.log(“结果”)';, 就会发现一直在打印“结果”
setInterval(function(){
fnTime(function(hour,minute,second){
console.log(hour+":"+minute+":"+second);
});
},
1000
);
function fnTime(callback){
var date = new Date();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
callback&&callback(hour,minute,second);
}