【发布时间】:2014-07-21 11:07:57
【问题描述】:
我想让浏览器等待任何通知,但如果单击链接则停止等待。
这就是为什么我有一个 PHP 脚本,它作为 while 循环,如果发生某些事情就会结束,然后返回事件。
PHP
require 'required.php';
ignore_user_abort ( false ); // I hoped this does it
set_time_limit ( 60 );
$lock = false; // maybe this helps?
register_shutdown_function ( function () {
// click the stop button in your browser ...
$lock = true;
exit ();
} );
if (isset ( $_SESSION ['user'] )) {
while ( ! new_event () && !$lock) {
if (connection_status () != CONNECTION_NORMAL) {
break; // and maybe this works???
}
sleep(1);
}
echo json_encode (array('someparameter','...') );
} else {
echo 'login first';
}
我的 JQuery AJAX 代码在加载后开始等待,并在单击链接后调用 abort()
var pollReq;
$(document).ready(function() {
doPoll(); // do the first poll
});
function doPoll() {
pollReq = $.post("wait.php", {parameter: value}, function(result) {
// new event, do something
doPoll();
// this effectively causes the poll to run again as
// soon as the response comes back
}, 'json');
}
如果链接被点击
$('a[href]').not('noajax').click(
function(e) {
e.preventDefault();
if (typeof pollReq !== 'undefined') {
pollReq.abort(); // stop waiting
}
// Do something...
});
我找不到错误/原因,也许有人找到了。谢谢
【问题讨论】:
标签: javascript php jquery ajax comet