如何在等待用户输入的同时暂停jQuery.ech()循环


How can I pause a jQuery .each() loop, while waiting for user input?

在继续迭代之前,我在获取jQuery .each loop以等待用户输入时遇到问题。这是我的代码(我已经评论了我希望发生的事情):

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="common_components.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="slider.css">
<?php
include_once 'common_components.php';
$query = $dbh->query("SELECT movies_dir FROM settings");
$row = $query->fetch(PDO::FETCH_ASSOC);
date_default_timezone_set('UTC');
echo "<title>Movie Scanner</title>";
?>
<script src="jquery.min.js"></script>
<script>
var retval = "not done";
function EnterManually() {
alert("Entering Data Manually"); //data has been entered manually so exit function & move ontop next index in loop in document.ready.
retval = "done";
}
function insertIntoDB(id,path) {
var request = $.ajax({
url: "update_movie.php?id="+id+"&path="+path,
type: "GET",            
dataType: "html"
});
request.done(function(data) {
if (data == 0) {
alert("Movie Succesfully Inserted"); //data has been entered sucessfully (update movie returns 0) so exit this function & search_file function & move ontop next index in loop in document.ready.
retval = "done";
}
else if (data == 1) {
alert("Unable to insert movie!"); //update_movie.php returns an error (returns 1), so exit this function but DON'T exit search_file function - wait for user to click on another set of details.
retval = "not done";
}
});
}
var files = <?php echo json_encode(preg_grep('/^([^.])/',scandir(realpath($row["movies_dir"]))))?>;
function Search_file(path) {
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","search_file.php?path="+path,false); //load search_file.php with movie
xmlhttp.send(); //send file off
$('#result').empty();   //clear div of content
document.getElementById("result").innerHTML=xmlhttp.responseText; //load output into page
}
</script>
</head>
<body>
<div id="result">
<script>
$(document).ready(function() {
$.each(files, function(key, value) {
Search_file("<?php echo addslashes(realpath($row["movies_dir"]))?>"+"''"+value); //iterate to next item in array & run search_file function
});
});
</script>
</div>
</body>
</html>

从本质上讲,"files"变量包含一个文件路径的JSON数组。对于数组中的每个索引,Search_file function都在该路径上运行,该路径应该显示该文件的搜索结果列表-调用&显示CCD_ 3页面。

当用户点击显示列表中的任何结果时,它应该尝试(通过调用update_movie.php文件的InsertIntoDB函数)将记录插入数据库。

如果插入成功(update_movie返回0)(或者EnterManually函数被触发),那么所有函数都应该退出,.each循环应该进入下一次迭代,重新开始整个过程。

如果插入函数不成功(因此update_movie将返回1),则不应该发生任何事情——.each循环不应该递增。相反,我们应该等待用户点击另一个结果。

目前,我无法让.each循环等待用户点击结果,它只是一直递增,直到循环中的最后一个项目。

您可以一次浏览一个文件。

var index = 0;
$("#result").on("click", function(){
    Search_file("<?php echo addslashes(realpath($row["movies_dir"]))?>"+"''"+files[index]);
    index++;
});

这个想法是,每次单击result的div,它都会加载下一个文件。我没有包含检查限制的代码,诸如此类的东西。