I have a simple script where an Ajax request is made every 2 seconds. If the data fetched from a php page called "ext" is different from the last data acquired, the code runs a function: in order to do this I use the ifModified:true setting on the ajax request, but it doesn't work.
我有一个简单的脚本,每2秒发出一次Ajax请求。如果从名为“ext”的php页面获取的数据与上次获取的数据不同,则代码运行一个函数:为了做到这一点,我在ajax请求中使用ifModified:true设置,但它不起作用。
This is the code I am using
这是我正在使用的代码
$(document).ready(function(){
setInterval(repeat,2000);
function repeat(){
$.ajax({
type: "GET",
url: "ext.php",
ifModified: true,
success:function(data) {
$('.photo').fadeOut(5000).delay(1000).css('background-image', 'url(' + data + ')' ).fadeIn(5000);
}
});
}
});
and this is the code found in the "ext.php": its aim is to simply output the path to the last added jpg into a directory.
这是“ext.php”中的代码:它的目的是简单地将最后添加的jpg的路径输出到目录中。
<?php
$array = glob("images/{*.jpg,*.JPG}", GLOB_BRACE);
$ultimo = end($array);
?>
<?php echo $ultimo; ?>
At the present time the script sets an image, whose path is found in ext.php, as the background of the div with class ".photo" with a fadeOut and a fadeIn. My idea is that when the path is the same as the previous used, the image shouldn't repeat the fadeOut and fadeIn but just stay still until a new path is provided.
目前,脚本设置一个图像,其路径在ext.php中找到,作为div的背景,类为“.photo”,带有fadeOut和fadeIn。我的想法是,当路径与之前使用的路径相同时,图像不应重复fadeOut和fadeIn,而是保持静止直到提供新路径。
What am I missing?
我错过了什么?
1 个解决方案
#1
4
When an Ajax request is completed successfully, the "success" callback is invoked with 2 arguments: the data received and a status string with the value "success". (the 3rd argument is irrilevant for this problem so I omitted it)
成功完成Ajax请求后,将使用2个参数调用“success”回调:收到的数据和值为“success”的状态字符串。 (第三个参数与此问题无关,所以我省略了它)
When you set the property "ifModified" to an Ajax request, on a second request on the same URL the success callback is invoked with 2 arguments: a undefined value and a status string with the value "nonmodified". (the 3rd argument is irrilevant for this problem so I omitted it)
当您将属性“ifModified”设置为Ajax请求时,在同一URL上的第二个请求上,将使用2个参数调用成功回调:未定义的值和值为“nonmodified”的状态字符串。 (第三个参数与此问题无关,所以我省略了它)
Pratically, in the success callback you have to check the "status" value (the second argument): if it is "success" then proceed, if it is "nonmodified" you don't do anything.
实际上,在成功回调中你必须检查“状态”值(第二个参数):如果它是“成功”然后继续,如果它是“未修改”你不做任何事情。
success: function(data, status) {
if (status === "success") {
// New data received, do what you want
} else {
// Old data received, ignore or similar
}
}
#1
4
When an Ajax request is completed successfully, the "success" callback is invoked with 2 arguments: the data received and a status string with the value "success". (the 3rd argument is irrilevant for this problem so I omitted it)
成功完成Ajax请求后,将使用2个参数调用“success”回调:收到的数据和值为“success”的状态字符串。 (第三个参数与此问题无关,所以我省略了它)
When you set the property "ifModified" to an Ajax request, on a second request on the same URL the success callback is invoked with 2 arguments: a undefined value and a status string with the value "nonmodified". (the 3rd argument is irrilevant for this problem so I omitted it)
当您将属性“ifModified”设置为Ajax请求时,在同一URL上的第二个请求上,将使用2个参数调用成功回调:未定义的值和值为“nonmodified”的状态字符串。 (第三个参数与此问题无关,所以我省略了它)
Pratically, in the success callback you have to check the "status" value (the second argument): if it is "success" then proceed, if it is "nonmodified" you don't do anything.
实际上,在成功回调中你必须检查“状态”值(第二个参数):如果它是“成功”然后继续,如果它是“未修改”你不做任何事情。
success: function(data, status) {
if (status === "success") {
// New data received, do what you want
} else {
// Old data received, ignore or similar
}
}