javascript每5秒轮询一次php脚本
如何使用javascript检查php脚本中的值以进行更改 我想做一些类似的事情:javascript每5秒轮询一次php脚本,javascript,php,Javascript,Php,如何使用javascript检查php脚本中的值以进行更改 我想做一些类似的事情: <script type="text/javascript"><!-- var innitialID = get_file_contents(lastPresentID.php); //get_file_contents(lastPresentID.php); function myTimeout() { var freshestID = get_file_contents(lastP
<script type="text/javascript"><!--
var innitialID = get_file_contents(lastPresentID.php); //get_file_contents(lastPresentID.php);
function myTimeout() {
var freshestID = get_file_contents(lastPresentID.php);
if(freshestID != innitialID) {
location.reload();
}
setTimeout(myTimeout, 5000);
}
window.onload = myTimeout;
</script>
window.onload=doAjax(true);
设置间隔(doAjax,5000);
var initialID=-1,freshstid;
函数doAjax(初始值){
var-xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();//IE7+、Firefox、Chrome、Opera、Safari的代码
}否则{
xmlhttp=newActiveXObject(“Microsoft.xmlhttp”);//IE6、IE5的代码
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
if(首字母){
initialID=xmlhttp.responseText;
}否则{
freshstid=xmlhttp.responseText;
if(initialID!=freshstid)
location.reload();
}
}
}
open(“GET”,“lastPresentID.php”,true);
xmlhttp.send();
}
我们开始了,在那里更新了ajax调用。首先设置initialId变量,然后间隔代码每5秒运行一次,更新FreshStId变量,并进行比较
编辑:代码现在不重复。下面加载的代码将分配oldVal
。然后使用setInterval
将每隔5秒更新newVal
,并检查newVal!=oldVal
它使用从lastPresentID.php
获取值
get\u file\u contents
不是JavaScript函数get\u file\u contents函数在做什么?它是一个带有ajax调用的js函数吗?好的,但是你知道如何用它代替真正的javascript吗?我很想学习如何正确地完成这件事你想看看叫做AJAX@CorCool:AJAX是JavaScript的一部分。这也正是你要我们做的。你的JavaScript书应该涵盖这一点;你用的是哪一种。大量代码重复。2.删除旧的错误答案。@AshleyMedway 1。OP没有使用任何ajax库,所以一点重复的纯javascript代码可能会帮助他学到一些东西。2.对于前面的答案,有一些评论可能对理解这个答案很有价值。3.如果你不想做建设性的工作,那就别再唠叨了:)我很有建设性,你不需要AJAX库来编写不重复的代码。请参阅:@AshleyMedway-这不起作用,您的代码无法知道它正在更新哪个变量。您只需提醒它们,但不设置oldvar和newvar,它们之间没有比较。对于希望学习一些实用JS的OP来说,这又回到了起点。@Husman我在我的页面底部尝试了这段代码。然而,这是行不通的。但这比我预想的要困难得多。。。哇!
<script type="text/javascript">
window.onload = doAjax(true);
setInterval(doAjax, 5000);
var initialID = -1, freshestID;
function doAjax(initial) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
if(initial) {
initialID = xmlhttp.responseText;
} else {
freshestID = xmlhttp.responseText;
if (initialID != freshestID)
location.reload();
}
}
}
xmlhttp.open("GET", "lastPresentID.php", true);
xmlhttp.send();
}
</script>
var oldVal = 0;
var newVal = 0;
function loadXMLDoc(old) {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 ) {
if(xmlhttp.status == 200){
if(old)
{
oldVal = xmlhttp.responseText;
}
else
{
newVal = xmlhttp.responseText;
if(newVal != oldVal)
alert("test");
}
}
else if(xmlhttp.status == 400) {
alert('There was an error 400')
}
else {
alert(xmlhttp.status)
}
}
}
xmlhttp.open("GET", "lastPresentID.php", true);
xmlhttp.send();
}
function timeout() {
loadXMLDoc(false);
}
loadXMLDoc(true);
setInterval(timeout, 5000);