我怎样才能暂停jQuery的每个()循环,同时等待用户输入? [英] How can I pause a jquery .each() loop, while waiting for user input?

查看:154
本文介绍了我怎样才能暂停jQuery的每个()循环,同时等待用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法得到一个jQuery的。每次循环等待用户输入,继续迭代之前。 这是我的code(我评论过什么,我想发生):

i'm having trouble getting a jquery .each loop to wait for user input, before continuing iterating. Here is my code (i've commented with what I would like to happen):

<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函数运行路径,其中应该显示搜索结果,该文件的列表上 - 主叫&安培;显示search_file.php页面。

Essentially, the 'files' variable contains a JSON array of file paths. for each index in the array, the Search_file function is run on that path, which should display a list of search results for that file - calling & displaying the search_file.php page.

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

When the user clicks on any of those results in the displayed list, it should attempt to insert (via the InsertIntoDB Function, which calls the update_movie.php file) a record into the database.

如果该插入成功(update_movie返回0)(或EnterManually功能被触发),那么该功能都应该退出,。每个循环应该进入下一个循环,重新开始整个过程​​了。

If that insert is successful (update_movie returns 0) (or the EnterManually function is triggered) then the functions should all exit, and the .each loop should move onto the next iteration, starting the whole process off again.

如果是插入功能不能全成(所以update_movie返回1),那么没有什么应该发生 - 每个回路应不会增加。相反,我们应该等待用户点击另外一个结果。

If that insert function is not successfull (so update_movie will return 1), then nothing should happen - the each loop should not increment. Instead, we should wait for the user to click on another result.

目前,我不能得到的。每次循环等待,直到用户点击的结果,它只是不断递增直线,直到在循环中的最后一个项目。

At the moment, I cannot get the .each loop to wait until the user has clicked on a result, it just keeps incrementing straight until the last item in the loop.

推荐答案

你可以只通过该文件一次。

you could just go through the files one at a time.

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

这个想法是,你点击搜索结果的股利将加载下一个文件每次。我不包括code,检查限制,之类的东西。

the idea is every time you click result's div it will load the next file. I didn't include code to check limit, and things like that.

这篇关于我怎样才能暂停jQuery的每个()循环,同时等待用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆