我遇到这种情况,我必须使用 JQuery 检查服务器上是否存在文件。我可以使用 Jquery Ajax 来执行此操作,但我需要一个监听器来监听服务器上文件是否存在,如果存在,则触发一些操作。 为了提供更多的意义,这里有一个例子,
client--> checks on server for file(time 0)--> not present
client--> checks on server for file(time 5)--> not present
client--> checks on server for file(time 10)--> file present--> return some message to client.
那么我如何实现一个监听器来检查服务器上的某些特定文件并在该文件可用时通知用户。
最佳答案
您可以使用ajax轮询..意味着以特定的时间间隔检查服务器。
您可以在java脚本中使用setInterval函数来调用特定函数。并在该函数中编写ajax请求。如果找到文件,只需清除计时器
查看示例代码
var timerForLoadingResult= setInterval(checkServerForFile,4000);//call the fnction in every 4 seconds.write ajax in that function.
function checkServerForFile() {
$.ajax({
type: "POST",
cache: false,
url: url,
success: function (result) {
if(check result) //if the file is on server
{
//do something
clearInterval(timerForLoadingResult) // clear timer
}
; }
});
}
关于jquery - 使用 Jquery 等待文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7554405/