나는 AJAX가있는 PHP 페이지에 casperJS 스크립트가 있으며, 버튼 클릭으로 트리거되고 이후 60 초마다 실행됩니다.브라우저 창을 닫은 후에도 setTimeout을 사용하여 AJAX를 계속 실행합니다.
브라우저 창을 닫을 수는 있지만 정지 버튼을 누를 때까지 계속 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?
ignore_user_abort()
언급하지만, 이들은 주로 자신의 스크립트를 가지고 있다는 사실에 초점에 게시물의 일부를 보았다 실행하는 데 오랜 시간이 걸리며 브라우저 창을 닫아 조기 종료를 막으려 고합니다.
현재 casperJS 스크립트를 사용하는 PHP 사이트가 설정되는 방식으로 .txt 파일에 대한 로그 업데이트와 오류 발생시 이메일을 수신하므로 스크립트가 있는지 확인할 수 있습니다. 실행 여부.
AJAX 요청을 시작하고 브라우저 창을 빨리 닫으면 해당 AJAX 요청이 완료되고 .txt 파일에 로그인되지만 후속 setTimeout은 무시됩니다.
닫힌 창으로 돌아가서 AJAX 반환의 현재 상태를 볼 수 있고 그 시간에 스크립트를 중지 할 수 있기를 원합니다 (마치 한번도 떠나지 않은 것처럼/닫았습니다. 브라우저 창). 이것이 가능한가?
내 현재 PHP 내 AJAX 요청 코드 :
<!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를 실행하는 것과 같습니다. – zerkms
다른 언어로 된 유사한 질문 : http://stackoverflow.com/questions/23573393/how-to-manage-server-side-processes-using-mysql/23577751#23577751 –