2014-10-28 82 views
0

在继续迭代之前,我无法让jQuery .each loop等待用户输入。 这里是我的代码(我和我想发生什么评论):如何在等待用户输入的同时暂停jQuery .each()循环?

<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> 

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

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

如果插入成功(update_movie返回0)(或EnterManually功能被触发),那么该功能都应该退出,而.each循环应该移动到下一个迭代,再次启动的整个过程了。

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

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

+0

JavaScript中没有wait(),所以你不能做你想做的事。您必须将其分解并创建一个在您调用下一步时迭代的方法。 – epascarello 2014-10-28 17:27:37

+0

请在此发布您的代码,以便将来访问者保存为SO。 – 2014-10-28 17:28:26

回答

0

您可以一次只读一个文件。

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

这个想法是每次点击结果div都会加载下一个文件。我没有包含检查限制的代码,以及类似的东西。