简体   繁体   中英

Error: timers.js:234 callback.apply(timer, args);

Just for testing, I want to create a file every two seconds, it managed to create the first file, but after 2 sec I get follow error:

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)

Everything is running in a Node server with restify framwork. If anyone knows the error would be much apprecited for helping! Here's the Code:

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);

You dont need to call function in setInterval . You need only pass reference to it. Also you need to define date inside of createFile function.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM