7
節點v4.2.6timer._repeat不是函數嗎?
我有一個小節目:
var chokidar = require('chokidar');
var _ = require('lodash');
var q = require('q');
var fs = require('fs');
var faker = require('faker');
var file = 'testfile.txt';
var pending = '';
var writer;
fs.open(file,'w+',function(){
writer = fs.createWriteStream(file);
writer.on('error',function(err){
console.log(err);
});
writer.on('drain',rewrite);
var timer = setInterval(write(),100);
});
function write(){
var data = faker.Helpers.createCard();
console.log('attempting to write ' + Buffer.byteLength(data,'utf8') + ' bytes');
var ok = writer.write(data.toString());
if(!ok){
console.log('buffer full, saving in memory');
pending += data;
}else{
console.log('written');
}
}
function rewrite(){
console.log('buffer empty, writing ' + Buffer.byteLength(data,'utf8') + ' bytes from memory');
var ok = writer.write(pending);
if(!ok){
console.log('buffer full again. Saving in memory again');
pending += data;
}
}
之後將其寫入文件中的前幾個字節,它的錯誤了與:
timers.js:275
timer._repeat();
^
TypeError: timer._repeat is not a function
at wrapper [as _onTimeout] (timers.js:275:11)
at Timer.listOnTimeout (timers.js:92:15)
什麼錯和我該如何糾正它?
您應該將'write'而不是'write()'傳遞給'setInterval()'。這不是直接的問題;什麼是「timers.js」? – Pointy
@有點哦,是的。好點 – Houseman