jQuery - 加载数据后执行函数

时间:2012-04-25 14:15:48

标签: jquery document-ready

我有一个将HTML导出为PDF的页面。页面中几乎没有用户控件使用AJAX请求加载数据。加载数据后,我必须将生成的页面发送到第三部分工具,该工具将发送的HTML导出为PDF,这是通过调用JS函数并将页面体作为参数发送完成的。

一切正常,但我必须在document.ready事件中延迟执行将数据发送到第三方工具5秒,否则它会发送空白页。如果我将间隔设置为5秒,它可以正常工作。

我想知道是否有任何我错过的事件可以通知我该页面已加载数据。

1 个答案:

答案 0 :(得分:0)

setTimeout将在一定的毫秒数到期时执行一个函数

$(document.ready(function() {
  function redirect()
  {
    // do redirection
  }

  setTimeout(redirect, 5000);
});

虽然..

$.ajax({
  url: 'something.pdf',
  dataType: 'application/pdf',
  success: function() {
    // though you are safer and better doing it when the ajax event has actually fired
  },
});