weixin_33714884 2017-03-01 10:40 采纳率: 0%
浏览 24

保存以前的AJAX响应

I'm doing AJAX-requests within an Intervalfunction in Javascript (e.g. once every 10 seconds).

I want to store the response (text ~ of the previous request) in a Javascript variable.

When the next request takes place, I want to check if the new response matches the previous one or not.

Something like:

var temp_data = response; //store response here
var final_data;

if(){
 final_data = temp_data;
} 
else {

}

Need soms advice/help,

Thanks!

  • 写回答

1条回答 默认 最新

  • 胖鸭 2017-03-01 12:42
    关注

    Solved by storing responses in an array and slicing existing and pushing new object.

    if (ajaxResults.length < 2) { //As long as arraylength < 2
       ajaxResults.push(data); //Add object (executed for 2 objects)
    } else if (ajaxResults.length == 2) { //
       ajaxResults.splice(0, 1); //Remove 1 object at index 0, next element that was in array goes to index 0
       ajaxResults.push(data); //Add last response to the end of array with push() = at index 1
    }
    
    评论

报告相同问题?