var timeout_wrapper = function (req) {
    return function () {
        // do some logging, cleaning, etc. depending on req
        req.abort();
    };
};

但这种可能不够灵活,更高级的:

var timeout_wrapper = function (req) {
    return function () {
        // do some logging, cleaning, etc. depending on req
        req.abort();
    };
};

var request = http.get(options, function (res) {
    res.on('data',function (data) {
        file.write(data);
        // reset timeout
        clearTimeout(timeout);
        timeout = setTimeout(fn, 10000);
    }).on('end',function () {
            // clear timeout
            clearTimeout(timeout);
            file.end();
            console.log(file_name + ' downloaded ');
            cb(null, file.path);
        }).on('error', function (err) {
            // clear timeout
            clearTimeout(timeout);
            console.log("Got error: " + err.message);
            cb(err, null);
        });
});

// generate timeout handler
var fn = timeout_wrapper(request);

// set initial timeout
var timeout = setTimeout(fn, 10000);

相关文章:

  • 2022-12-23
  • 2021-12-29
  • 2021-12-24
  • 2021-10-18
  • 2021-06-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
  • 2021-12-19
  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案