简体   繁体   English

修复javascript中的变量增量

[英]fix variable increment in javascript

I'm coding a wordpress theme and I want to increment a load more button.我正在编写一个 wordpress 主题,我想增加一个加载更多按钮。 I'm not using wordpress always and this is the first time I've this problem with a javascript variable.我并不总是使用 wordpress,这是我第一次遇到 javascript 变量的问题。 The variable pull_page in fact will not increment and every time the script will run it will fetch only two pages.变量pull_page实际上不会增加​​,每次脚本运行时它只会获取两个页面。 Is there any error in the code, and how I can fix it?代码中是否有任何错误,我该如何解决?

$('#load-more').on('click', function(e){
  e.preventDefault();
  var pull_page = 1;
  var jsonFlag = true;

  if(jsonFlag){
    pull_page++;

    $.getJSON("/beta/wp-json/portfolio/all-posts?page=" + pull_page, function(data){
        if(data.length){
        $.each(data, function(i, item){
          var html = '<div class="card">';
          html += '<a href="'+ item.permalink +'">';
          html += '<img class="card-img-top w-100" src="'+ item.featured_img_src +'" id="case-studies" />';
          html += '<div class="overlay"><h4 class="text-center" id="client-name">'+ item.title +'</h4></div>';
          html += '</a>';
          html += '</div>';
          $('body').find('.card-columns')
          .append(html);
        });
      }
      else{
        jsonFlag = false;
      }
    }).done(function(data){
      if(data.length >= 4){
        jsonFlag = true;
      }
      else{
        jsonFlag = false;
      }
    });
    }

  }); // end load more

You're resetting pull_page to 1 each and every time the load more button is clicked.每次单击加载更多按钮时,您都会将pull_page重置为 1。

Move it outside.把它移到外面。

var pull_page = 1;
$('#load-more').on('click', function(e) {
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM