一.重点代码
声明对象缓存池变量,在ajax请求时以page页数作为键名存储,再在click事件中判断需要ajax还是缓存池
function getAjaxData(){
$.ajax({
url: 'http://study.jsplusplus.com/Lfcourses/getCourses',
type: 'POST',
dataType: 'JSON',
data: {
page: page
},
success: function(data){
oLoading.style.display = 'block'
t = setTimeout(function(){
oLoading.style.display = 'none'
cache[page] = data;
render(data)
},500)
},
error: function(){
alert('获取错误')
}
})
}
function getCacheData(){
var data = cache[page];
render(data);
}
cache[page] ? getCacheData() : getAjaxData();
function ajaxReturn(opt){
$.ajax({
url: opt.url,
type: 'POST',
dataType: 'JSON',
data: opt.data,
timeout: 100000,
success: opt.success,
error: opt.error
})
}
function getAjax(){
ajaxReturn({
url: ...,
success: ....
})
}