Javascript - 如何创建创建动态元素的承诺

时间:2017-03-28 14:07:52

标签: javascript es6-promise

我想在创建了我要滚动到的部分的所有元素之后执行scroll to函数。我认为promises将是最佳选择。这是我尝试过的但是它不起作用,不确定如何在不使用ajax请求时以正确的方式编写promises: 这是脚本:

var showMagazineDetail = function showMagazineDetail(id, slug, name, summary, issueImage, magazineImage, page){
    images = [];
    nextImage = 0;
    imagesIndex = 0;
    loadedImages = [];
    scrollPoint = document.height;

    window.location.hash = slug;

    let createMagazineDetails = new Promise((resolve, reject) => {

      $(".magazine-section").css('visibility', 'visible');

      $('#name').text(name);
      $('#summary').text(summary);
      $('#cover-image').attr({"src" : '/imagecache/cover/' + issueImage, "alt" : name});

      if (magazineImage != '') {
        $('#issueImage').show();
        $('#issueImage').attr({"src" : '/imagecache/medium/' + magazineImage, "alt" : name});
      }else {
        $('#issueImage').hide();
        $('#issueImage').attr({"src" : '', "alt" : name});
      }

      // $('body').css('overflow-y', 'scroll');

      $.getJSON("issue/images",
          { id: id },
          function(result){
            if (result.length < 2){
                $('.magazine-preview-nav').hide();
            } else {
                $('.magazine-preview-nav').show();
            }
            $.each(result, function(i, value){
                images.push(value);
            });
            preload();
      });

    });

    createMagazineDetails.then(() => {
      $('html,body').animate({
         scrollTop: $(".magazine-detail").offset().top - 80
      });
    });
};

 preload = function() {
      for (i = 0; i < 4; i++) {
          if (nextImage < images.length) {
              var img = new Image();
              img.src = '/imagecache/cover/' + images[nextImage];
              loadedImages[nextImage] = img;
              ++nextImage;
          }
      }
  }

0 个答案:

没有答案