只是为了测试,我想每两秒创建一个文件,它设法创建第一个文件,但是在2秒之后我得到了关注错误:
Restify listening on port 8080 in %s mode
create 2012_8_5_8_57.txt
timers.js:234
callback.apply(timer, args);
^
TypeError: Cannot call method 'apply' of undefined
at Timer.exports.setInterval.timer.ontimeout (timers.js:234:14)
所有东西都在节点服务器上运行,并且可以解析框架。如果有人知道错误将非常感谢帮助! 这是代码:
var d = new Date();
var n = d.getDay();
var m = d.getMonth();
var y = d.getFullYear();
var min = d.getMinutes();
var sec = d.getSeconds();
//create file
function openUnlink(name) {
console.log(name);
fs.open(name, 'w', function (err) {
console.log(' create ' + name);
});
};
function createFile() {
openUnlink(y+'_'+m+'_'+n+'_'+min+'_'+sec+'.txt');
};
//create file every 2 seconds
setInterval(createFile(), 2000);
答案 0 :(得分:4)
您无需在setInterval
中调用函数。您只需要传递参考。您还需要在createFile
函数内定义日期。
var fs = require('fs');
//create file
function openUnlink(name) {
console.log(name);
fs.open(name, 'w', function (err) {
console.log(' create ' + name);
});
};
function createFile() {
var d = new Date();
var n = d.getDay();
var m = d.getMonth();
var y = d.getFullYear();
var min = d.getMinutes();
var sec = d.getSeconds();
openUnlink(y+'_'+m+'_'+n+'_'+min+'_'+sec+'.txt');
};
//create file every 2 seconds
setInterval(createFile, 2000);