【问题标题】:How to execute a method at background using Jquery?如何使用 Jquery 在后台执行方法?
【发布时间】:2014-05-16 08:44:56
【问题描述】:

我正在使用第三方控件在其中加载 pdf,其中 Pdf 是延迟加载的。在我的场景中,我想在页面加载时加载几个页面,并希望加载 pages to load at background.

的其余部分
// Load the required pages at the Load.
function PreLoadPdf(startpage, endpage) {
    myApi.addEventListener("rendered", function () {
        for (var i = startpage; i <= endpage ; i++) {
            myApi.getPage(i).loadRendered();
        }
    });
} 

现在上面的代码执行预加载我想要的 Pdf,但我也想在后台加载剩余的页面。 How to execute the Jquery Method at the background of the page without freezing the page. 而且我不应该使用,

setTimeout(function () {}

【问题讨论】:

  • 如何,实际上用户并不关心加载,他只是查看阅读器中的某些页面,而其余页面应在后台加载

标签: javascript jquery asp.net pdf radpdf


【解决方案1】:

有很多方法可以解决这个问题,但我相信最好的解决方案是使用 jQuery 的promise API。

//Untested code... typed on the fly
//May have minor syntax errors.

var fileList = [
  "http://example.com/file1.pdf",
  "http://example.com/file2.pdf"
];


var promiseArray = [];

// Fill the file promise array
for (var i = 0; i < fileList.length; i++) {
    var promise = $.get(fileList[i]);
    promiseArray.push(promise);
  });
}

$.when.apply($, promiseArray).then(function() {    
  // All have been resolved (or rejected), do your thing    
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多