我每隔2秒就从php页面获取数据当我调用一次数据时数据非常大然后数据出现但是当我将我的代码放入setinterval函数时,控制台中的数据没有显示我将此代码放入setinterval函数,因为每2秒我需要新的数据任何想法plz共享
var data_array = '';
setInterval(function () {
$.ajax({
url:"./phponline.php",
async:false,
success:function(res)
{
data_array = res;
},
error:function(errorMsg)
{
}
});
}, 5000);
console.log(data_array);
答案 0 :(得分:0)
您的console.log
应该在成功回调中调用,或者直接在setInterval回调中的ajax调用之后调用。
如果您在console.log
之后放置setInterval
,data_array
将为空,因为它已在5秒后设置。
var data_array = '';
setInterval(function () {
$.ajax({
url:"./phponline.php",
async:false,
success:function(res)
{
data_array = res;
console.log(data_array);
},
error:function(errorMsg)
{
}
});
}, 5000);
答案 1 :(得分:0)
正如Muhammed Atif在评论中所说,您需要将控制台日志放在SetInterval函数中。
var data_array = '';
function handleData() {
console.log(data_array);
}
setInterval(function () {
$.ajax({
url:"./phponline.php",
async:false,
success:function(res)
{
data_array = res;
handleData();
},
error:function(errorMsg)
{
}
});
}, 5000);
答案 2 :(得分:0)
你需要在payment :: [(Integer , Integer)] -> Integer -> [(Integer , Integer)] -> Maybe [(Integer , Integer)]
payment portemonnaie amount cashier | Nothing <- mayHavePaid = Nothing
| Just paid <- mayHavePaid, content paid - amount == 0 = Just []
| otherwise = payExact cashier (content paid - amount)
where
mayHavePaid = pay portemonnaie amount
的ajax成功中调用一些自定义函数来提供存储在data_array中的响应的效果:
setInterval
答案 3 :(得分:0)
这里有几个问题,主要问题是你正在尝试进行同步ajax调用,而且这个问题已被弃用。您需要将其作为异步调用来处理...
每次将新数据输入函数并在成功回调中调用该函数时,将要运行的代码放入
var data_array = ''; // this is a global variable
function getNewData() {
$.ajax({
url: "./phponline.php",
})
.done(function(res) {
data_array = res; // the global variable is updated here and accessible elsewhere
getNewDataSuccess();
})
.fail(function() {
// handle errors here
})
.always(function() {
// we've completed the call and updated the global variable, so set a timeout to make the call again
setTimeout(getNewData, 2000);
});
}
function getNewDataSuccess() {
console.log(data_array);
}
getNewData();
正如我在评论中所解释的那样,将setInterval
与ajax调用一起使用是一个坏主意,因为如果调用时间超过间隔,则最终可能会重叠调用。此方法进行调用,等待结果,然后使用setTimeout
代替进行下一次调用。通过这种方式,您可以在最后一次完成后的2秒内进行ajax调用。