使用函数存储每个 ajax 调用数据
use a function to store each ajax call data
我想在 ajax 次调用之间存储数据,这样我就可以将累积的数据存储在一个对象中。
我可以获得每个调用的数据,但我无法将数据累积到一个完整的数组中。
我创建了 dataAccumulator ()
函数,这样当我调用 accumData.append
时,单个调用的数据可以推送到 holder 数组。
无论我在哪里声明或调用函数,我都会收到错误 Cannot read property 'append' of undefined
或 Uncaught TypeError: dataAccumulator is not a function
...
这是代码:
var inter;
var dataAccumulator = dataAccumulator(); //object of interest
function startTempMonit()
{
$(document).ready(function()
{
time= 0;
inter = setInterval(function()
{
$.ajax // ajax call starts
({
//not relevant for the question. arguments removed...
})
.done(function(data) {
//problem here. data is foreach call.
//I would like to accumulate each of the data to handle the whole here.
dataAccumulator.append(data);
});
time= time + 0.5;
}, 500)
});
};
function dataAccumulator () {
let accumData = [];
console.log("accumData function created")
function append(data) {
accumData.push(data);
console.log(accumData); //log the accumulated data each time
}
this.append = append;
};
我想我的问题出在 js 作用域上。我想在 ajax 调用 .done
中保留我的 accumData
数组。这就是我的问题的总结。
第一步总是摆脱全局变量。第二步是使用事件而不是全局状态。
注册每次数据到达时调用的回调。然后在回调中处理数据。
function startTempMonit(callback) {
var time = 0;
return setInterval(function () {
$.ajax({
url: '....',
data: '.....' + tiempo
dataType: '.....',
})
.done(function (data) {
console.log("received", data);
callback(data);
});
time += 0.5;
}, 500);
}
$(function () {
var inter, accumData = [];
$("#buttonStart").click(function () {
inter = startTempMonit(function (data) {
accumData.push(data);
// do something, i.e. add it to a table or a chart
});
});
$("#buttonStop").click(function () {
clearInterval(inter);
});
});
我想在 ajax 次调用之间存储数据,这样我就可以将累积的数据存储在一个对象中。 我可以获得每个调用的数据,但我无法将数据累积到一个完整的数组中。
我创建了 dataAccumulator ()
函数,这样当我调用 accumData.append
时,单个调用的数据可以推送到 holder 数组。
无论我在哪里声明或调用函数,我都会收到错误 Cannot read property 'append' of undefined
或 Uncaught TypeError: dataAccumulator is not a function
...
这是代码:
var inter;
var dataAccumulator = dataAccumulator(); //object of interest
function startTempMonit()
{
$(document).ready(function()
{
time= 0;
inter = setInterval(function()
{
$.ajax // ajax call starts
({
//not relevant for the question. arguments removed...
})
.done(function(data) {
//problem here. data is foreach call.
//I would like to accumulate each of the data to handle the whole here.
dataAccumulator.append(data);
});
time= time + 0.5;
}, 500)
});
};
function dataAccumulator () {
let accumData = [];
console.log("accumData function created")
function append(data) {
accumData.push(data);
console.log(accumData); //log the accumulated data each time
}
this.append = append;
};
我想我的问题出在 js 作用域上。我想在 ajax 调用 .done
中保留我的 accumData
数组。这就是我的问题的总结。
第一步总是摆脱全局变量。第二步是使用事件而不是全局状态。
注册每次数据到达时调用的回调。然后在回调中处理数据。
function startTempMonit(callback) {
var time = 0;
return setInterval(function () {
$.ajax({
url: '....',
data: '.....' + tiempo
dataType: '.....',
})
.done(function (data) {
console.log("received", data);
callback(data);
});
time += 0.5;
}, 500);
}
$(function () {
var inter, accumData = [];
$("#buttonStart").click(function () {
inter = startTempMonit(function (data) {
accumData.push(data);
// do something, i.e. add it to a table or a chart
});
});
$("#buttonStop").click(function () {
clearInterval(inter);
});
});