Reputation: 11756
To consolidate a few SQL calls I'm trying to make one query to the server and then have the client side iterate through each result. The caveat is that I need to wait for user input before processing the next result. Is this possible?
I have a jquery call similar to below:
$.post('functions/file_functions.php', {type: 'test', f: 'load'}, function(data) {
if (data.success) {
$.each(data.files, function() {
// Can I wait for user input at this point and then process the next
// file after a users interaction (e.g. a button click)?
});
}
}, "json");
Upvotes: 3
Views: 263
Reputation: 20753
One of the ways to have "blocking" user input in javascript is to call window.prompt (among others like window.confirm, or window.showModalDialog). However its not really customizable, you might want to just save the data
coming back from the server and have some kind of a user input event based processing.
In code it would look like this:
var the_answer = window.prompt("What's the airspeed velocity of an unladen swallow?");
console.log(the_answer);
Upvotes: 0
Reputation: 38345
I'm going to expand on my comment a bit, and hopefully make it a useful answer. JavaScript is single-threaded, so there's no way to block the execution of a function while waiting for something else (such as an element being clicked on) to happen. Instead, what you could do is store the list of files into an array when the AJAX POST request returns successfully, then use a separate click
event handler to cycle through them (I assume getting one file per click).
Code may look something like this:
$(function() {
var files, index = 0;
$.post('functions/file_functions.php', {type: 'test', f: 'load'}, function(data) {
if (data.success) {
files = data.files;
}
}, "json");
$('.mybutton').click(function() {
if(files) {
var file = files[index++];
// do something with the current file
}
});
});
Upvotes: 4