Created
January 14, 2015 09:30
-
-
Save thierrypigot/f518d17c3694b85e2704 to your computer and use it in GitHub Desktop.
Permet de faire des traitement en Ajax, en particulier pour passer des problématiques de timeout.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<style type="text/css"> | |
body {font-family: verdana, arial; font-size: 62.5%; margin: 0 auto; width: 80%;background-color: #f5f5f5;} | |
#container {background-color: #fff;padding:10px;margin:20px 0;} | |
h1{font-size: 3em;} | |
h2{font-size: 2em;} | |
#loading{display:none;} | |
</style> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> | |
<script type="text/javascript"> | |
var id = 0; | |
$(document).ready(function() { | |
$('#loading').show(); | |
setTimeout(function self(){ | |
$.post("traitement.php", { endTime: "<?php echo time() + 10; ?>", id: id }, function(data) { | |
if ( data == "fin" ) { | |
$('#loading').hide(); | |
alert( "Traitement terminé" ); | |
} else { | |
var currentText = $('#my_response').html(); | |
document.getElementById('my_response').innerHTML = data + "\n"+ currentText; | |
id++; | |
setTimeout(self, 1000); | |
} | |
}); | |
}, 1000); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="container"> | |
<h1>Traitement en ajax</h1> | |
<span id="loading"><img src="data:image/gif;base64,R0lGODlhIwAjAMQAAP////f39+/v7+bm5t7e3tbW1s7OzsXFxb29vbW1ta2traWlpZycnJSUlIyMjISEhHt7e3Nzc2tra2NjY1paWlJSUkpKSkJCQjo6OjExMSkpKRkZGRAQEAAAAP///wAAACH/C05FVF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90baPBvS6MTgoKgqjxEBEihZuAsRAxHKJHJXk7NAwBB8RzsPRqBYFo4RgkFALKxMhAxAiKBdXtAXgah4Eis2nIBgcLCSgVGxMKNYAoD4MzAgI5KgQHCAhULQUKmgmRJgUaIhwWLwIEBQQDKQEKDxEQCXYxnSUBcjapKAIcHUg+JgkUHRx+YB6zIw4YEMc2QiMBzDB0HgbGvifR19rb3N3e3+Dh4ikhADs=" alt="loading" /></span> | |
<pre id="my_response"></pre> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Loading du bootstrap WordPress | |
/* | |
while( !is_file( 'wp-load.php' ) ) { | |
if( is_dir( '..' ) ) | |
chdir( '..' ); | |
else | |
die( 'EN: Could not find WordPress! FR : Impossible de trouver WordPress !' ); | |
} | |
require_once( 'wp-load.php' ); | |
*/ | |
/** Traitement à faire **/ | |
// !!! DEMO !!! | |
$time = time(); | |
$fin = $_POST['endTime']; // Variable envoyée par le script ajax.php | |
if( $time <= $fin ) // Si on a encore des traitement à faire | |
{ | |
// Faire un echo pour afficher un etat sur l'écran | |
echo $time .' / '. $fin; | |
flush(); | |
ob_flush(); | |
} | |
else // Sinon on retourne la chaine "fin" pour stoper le programme | |
{ | |
echo 'fin'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment