Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
794 views
in Technique[技术] by (71.8m points)

一个方法怎么只初始化一次

我有个分页接口,先要查total总条数在初始化分页组件,这个分页插件怎么只初始化一次,现在我用settime表面上搞定的,但是肯定不是这样,有思路吗?谢谢

$.ajax({
    ...
    ...
    success: function(data) {
        tot = data.data.total
    }
    ...
    ...
});

setTimeout(function(){
    $("#page").initPage(tot, 1, GG.kk);
},1000)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

你这个问题是异步造成的,建议你先去了解下异步的相关知识

$.ajax({
  ...
    ...
  success: function(data) {
    initPagination(data.data.total);
  }
    ...
    ...
});

let rendered = false;
function initPagination(total) {
  if (rendered) return;
  rendered = true;
  $("#page").initPage(total, 1, GG.kk);
}

你可以写一个函数放到ajax成功的回调里来拿total的数据


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...