【发布时间】:2014-07-27 06:35:31
【问题描述】:
我有一个带有 AJAX 的 PHP 页面请求另一个 PHP 页面,该页面包含一个 casperJS 脚本,由单击按钮触发,随后每 60 秒执行一次。
我希望能够关闭浏览器窗口但仍继续运行 AJAX 请求,直到按下停止按钮。
我看过一些关于这个主题的帖子
- Will a script continue to run even after closing a page?
- How can I make setInterval also work when a tab is inactive in Chrome?
- Scripting events no longer fire when a user leaves site or closes browser even though app is still active
他们提到了 PHP ignore_user_abort(),但这些主要关注这样一个事实,即他们的脚本需要很长时间才能执行,并且他们希望通过关闭浏览器窗口来防止提前终止。
目前,我使用 casperJS 脚本的 PHP 站点的设置方式,我会收到 .txt 文件的日志更新,以及在出现任何错误时发送的电子邮件,因此我可以查看脚本是否正在运行或不。
如果我启动 AJAX 请求并快速关闭浏览器窗口,则该 AJAX 请求将完成并登录 .txt 文件,但随后的 setTimeout 将被忽略。
我希望能够返回到关闭的窗口并仍然看到 AJAX 返回的当前状态,并且还能够在那个时候停止脚本(就好像我从未离开/关闭过浏览器一样窗户)。这可能吗?
我当前用于 AJAX 请求的 PHP 代码:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" media="screen, projection"/>
<script src="jquery-2.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript" src="jquery.dropdownPlain.js"></script>
<title>CasperJS Automated Testing Unit</title>
</head>
<center>
<body>
<div id="mainContent">
<p>Welcome to the CasperJS Automated Testing Unit</p>
<button id="button_AJAX">Run CasperJS</button>
<button id="button_STOP" onclick="myStopFunction()">Stop CasperJS</button>
</div>
<br>
<div id="loading"></div>
<script type="text/javascript">
$('#button_AJAX').click(function executecasperJS() {
$('#loading').html('<img src="rays.gif"><br><i>Web harvesting in progress; please wait for test results.</i>');
$.ajax({
type: "GET",
dataType: "text",
url: "phpwithCasperJS.php",
success: function (data) {
$('#loading').html(data);
}
});
timeout = setTimeout(executecasperJS,60000);
});
$("#button_AJAX").click(function() {$("#button_AJAX").text("CasperJS Executed");});
$("#button_STOP").click(function() {$("#button_AJAX").text("Run CasperJS");});
function myStopFunction() {
clearTimeout(timeout);
}
</script>
</div>
<div id="page-wrap">
<ul class="dropdown">
<li><a href="#">CasperJS Logs</a>
<ul class="sub_menu">
<li><a href="casperjs_log.txt" target="_blank">Testing Log</a></li>
<li><a href="casperjs_error.txt" target="_blank">Error Log</a></li>
</ul>
</div>
</center>
</body>
</html>
还有包含我的 casperJS 脚本的 PHP:
<?php
date_default_timezone_set('America/Managua');
$date = date('m/d/Y h:i:s a', time());
$time_start = microtime(true);
$output = exec("/usr/local/bin/casperjs /path/to/script/casperJScript.js");
if (strpos($output, 'Check for fail message') === FALSE) {
require_once('../class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "emailaddress";
$mail->Password = "password";
$mail->SetFrom('emailaddress');
$mail->AddReplyTo("emailaddress");
$mail->Subject = "casperJS: Server failure occured on $date";
$mail->Body = "Body";
$mail->AddAddress("emailaddress");
if(!$mail->Send()) {
} else {
echo '<span style="color:#FF0000">The server has gone 100% failure and an email with the error has been sent.</span>';
$myfile = fopen("../casperjs_error.txt", "a") or die("Unable to open file!");
$txt = "ERROR log: $output on $date" . PHP_EOL ;
fwrite($myfile, $txt);
fclose($myfile);
echo "<br />";
echo "<br />";
}
}
echo "Test Results: $output";
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<br />";
echo "<br />";
echo "Test completed in $time seconds\n on $date";
$myfile = fopen("../casperjs_log.txt", "a") or die("Unable to open file!");
$txt = "Log: $output on $date" . PHP_EOL ;
fwrite($myfile, $txt);
fclose($myfile);
?>
【问题讨论】:
-
"关闭浏览器窗口后" --- 就像要求在关闭的机器上运行 JS。
-
不同语言的类似问题:stackoverflow.com/questions/23573393/…
标签: javascript php jquery ajax casperjs