I have a PHP page with an AJAX requested to another PHP page which houses a casperJS script, triggered by a button click and subsequently executed every 60 secs.
I would like to be able to close the browser window but still continue to run AJAX requests, until the Stop button is pressed.
I have seen some of the posts on this topics
- 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
They mention PHP ignore_user_abort()
but these mainly focus on the fact that their scripts take a very long time to execute and they want to prevent early termination by closing browser window.
Currently, the way my PHP site with the casperJS script is set up, I receive log updates to a .txt file, as well as an email in case of any errors, so I am able to see if the script is running or not.
If I start the AJAX request and quickly close my browser window, that one AJAX request will finish and log in the .txt file, but the subsequent setTimeout is ignored.
I would like to be able to return to the window that was closed and still see the current status of the AJAX returns, and also be able to at that time Stop the script (as if I had never left/closed the browser window). Is this possible?
My current PHP code for my AJAX request:
<!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>
And the PHP which houses my casperJS script:
<?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);
?>