2014-04-04 19 views
0

我正在使用第三方控件来加载其中的PDF,其中Pdf是惰性加载。在我的情况我要加载的页面加载几页要加载现在上面的代码pages to load at background.如何使用Jquery在后台执行一个方法?

// 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() {}

+0

对于使用Ajax ... –

+0

如何,actully用户不没有关于装载他只是查看读者的某些页面,而页面的其余部分应在后台加载 –

回答

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  
}); 
相关问题