【发布时间】:2016-06-25 11:17:15
【问题描述】:
我有客户页面和 ajax 我正在加载关于他们是否向我们发送电子邮件的信息。
代码如下:
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'email';
$password = 'password';
$this->session->data['imap_inbox'] = $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
foreach($customers as $customer){
$emails = imap_search($inbox, 'FROM ' . $email);
// Processing info
}
但一页上大约有 20-30 位客户,因此显示过程有时需要大约 10-20 秒,我无法优化该过程。
但是当客户端尝试重新加载页面时,它仍在等待imap_search 完成,因此在重新加载时可能需要 20 秒才能真正重新加载页面。
我尝试使用beforeunload 函数中止ajax 并关闭imap,但这不起作用。
我的代码:
阿贾克斯:
$(window).bind('beforeunload',function(){
imap_email.abort(); // the ajax is succesfully aborted(as showed in console), yet the page still takes considerable time to reload
$.ajax({
type: 'GET',
url: 'getimapmails&kill=1',
async:false
}); // ajax call to the same function to call imap_close
});
PHP:
if($this->request->get['kill'] == '1'){
imap_close($this->session->data['imap_inbox']);
unset($this->session->data['imap_inbox']);
$kill == 1;
exit;
}
但是即使ajax 被中止并且imap_close 在变量持有imap_open 上被调用,页面重新加载仍然需要10-20 秒,所以我假设imap 没有关闭。
如何关闭imap 以便页面可以立即重新加载?
【问题讨论】:
-
您是否有任何 imap 日志可以在您刷新以查看它是否正在关闭连接时查看?通常感觉你现在需要对代码进行 imap 调整。
-
可能存在会话锁定问题;
标签: javascript php jquery ajax imap