我正在尝试使用Node.js的异步功能。
这是我的完整代码。
[ 0.000000] Memory: 2736316K/2883584K available (4796K kernel code, 488K rwdata, 1360K rodata, 448K init, 341K bss, 130884K reserved, 16384K cma-reserved)
[ 0.000000] Virtual kernel memory layout:
[ 0.000000] modules : 0xffffff8000000000 - 0xffffff8008000000 ( 128 MB)
[ 0.000000] vmalloc : 0xffffff8008000000 - 0xffffffbebfff0000 ( 250 GB)
[ 0.000000] .text : 0xffffff8008080000 - 0xffffff8008530000 ( 4800 KB)
[ 0.000000] .rodata : 0xffffff8008530000 - 0xffffff8008690000 ( 1408 KB)
[ 0.000000] .init : 0xffffff8008690000 - 0xffffff8008700000 ( 448 KB)
[ 0.000000] .data : 0xffffff8008700000 - 0xffffff800877a200 ( 489 KB)
[ 0.000000] .bss : 0xffffff800877a200 - 0xffffff80087cf76c ( 342 KB)
[ 0.000000] fixed : 0xffffffbefe7fd000 - 0xffffffbefec00000 ( 4108 KB)
[ 0.000000] PCI I/O : 0xffffffbefee00000 - 0xffffffbeffe00000 ( 16 MB)
[ 0.000000] vmemmap : 0xffffffbf00000000 - 0xffffffc000000000 ( 4 GB maximum)
[ 0.000000] 0xffffffbf00000000 - 0xffffffbf02c00000 ( 44 MB actual)
[ 0.000000] memory : 0xffffffc000000000 - 0xffffffc0b0000000 ( 2816 MB)
这是我的结果。
const fs = require('fs');
const request = require('request');
var schedule = require('node-schedule');
var getBTCData = function(callback){
request({
url: "https://api.gopax.co.kr/trading-pairs/BTC-KRW/book?level=1",
method: 'GET',
json :true
}, (error, response, body) => {
let date = new Date().toLocaleString();
let today = date.substring(0,4) + "0" + date.substring(5,6) +
date.substring(7,9);
let time = new Date().toLocaleString().substring(10,19);
let bid = body.bid[0][1];
let bidVolume = body.bid[0][2];
let ask = body.ask[0][1];
let askVolume = body.ask[0][2];
let data = `${time},BTC,${bid},${bidVolume},${ask},${askVolume}\n`;
callback(data, today);
});
}
setInterval(function(){getBTCData((data, today)=>
{fs.appendFileSync(`./gopax_BTC_${today}.csv`,data);})}, 1000);
我想这样每秒获取一次数据。
19:02:32,BTC,7392000,0.70893442,7408000,0.69
19:02:32,BTC,7392000,0.70893442,7408000,0.69
19:02:33,BTC,7392000,0.70893442,7408000,0.69
19:02:35,BTC,7392000,0.70893442,7408000,0.69
19:02:35,BTC,7392000,0.70893442,7408000,0.69
如何每秒钟获取和保存数据?
答案 0 :(得分:0)
根据我的理解,您可以这样做。
const fs = require('fs');
const request = require('request');
var schedule = require('node-schedule');
var getBTCData = function(callback){
request({
url: "https://api.gopax.co.kr/trading-pairs/BTC-KRW/book?level=1",
method: 'GET',
json :true
}, (error, response, body) => {
let date = new Date().toLocaleString();
let today = date.substring(0,4) + "0" + date.substring(5,6) +
date.substring(7,9);
let time = new Date().toLocaleString().substring(10,19);
let bid = body.bid[0][1];
let bidVolume = body.bid[0][2];
let ask = body.ask[0][1];
let askVolume = body.ask[0][2];
let data = `${time},BTC,${bid},${bidVolume},${ask},${askVolume}\n`;
callback(data, today);
});
}
setInterval(function(){getBTCData((data, today)=>
{
data.forEach(function(result) {
setTimeOut(function() {
fs.appendFileSync(`./gopax_BTC_${today}.csv`,result)
}, 1000)
});
;})}, 1000);