gpt4 book ai didi

javascript - PHP 在页面上退出 ajax abort()

转载 作者:行者123 更新时间:2023-11-30 17:22:34 24 4
gpt4 key购买 nike

我想让浏览器等待任何通知,但如果单击链接则停止等待。这就是为什么我有一个 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...
});

我找不到错误/原因,也许有人找到了。谢谢

最佳答案

经过多次尝试和搜索,我终于发现,这都是关于 session 的。感谢this帖子,我更改了 PHP,使其在 while loop

中像这样工作

PHP

while ( ! newMsg ( $chatID, $lastID ) ) {
// Did the connection fail?
if (connection_status () != CONNECTION_NORMAL) {
break;
}
session_write_close();
sleep(1);
session_start();
}

如果你有更好的解决方案,我很乐意看到。

编辑:这之所以有效,是因为它允许另一个脚本在其间的 1 秒内获取 session lock()。

如果您不需要向 session 写入任何内容:将 session_write_close(); 放在 while 循环之前并省略 session_start();

关于javascript - PHP 在页面上退出 ajax abort(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24863620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com