php - 服务器端查询后如何等待客户端的输入?

标签 php jquery

为了整合一些 SQL 调用,我尝试对服务器进行一次查询,然后让客户端遍历每个结果。需要注意的是,在处理下一个结果之前,我需要等待用户输入。这可能吗?

我有一个类似于下面的 jquery 调用:

$.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");

最佳答案

我将稍微扩展一下我的评论,并希望它成为一个有用的答案。 JavaScript 是单线程的,因此无法在等待其他事情(例如单击元素)发生时阻止函数的执行。相反,您可以做的是在 AJAX POST 请求成功返回时将文件列表存储到一个数组中,然后使用单独的 click 事件处理程序循环遍历它们(我假设每次点击获取一个文件) .

代码可能看起来像这样:

$(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
        }
    });

});

关于php - 服务器端查询后如何等待客户端的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11796291/

相关文章:

javascript - 仅当在网站中到达该部分时才显示效果

javascript - 如何获取在其他 svg 上绘制的 svg 的 id 并将其删除

javascript - 如何在单个页面上创建滚动到顶部按钮?

php - CodeIgniter 意外的 session 轮换

php - 在 Cloud Foundry 上部署开源 CMS 'my little forum'(需要 PHP 和 MySQL)

php - "Cannot add or update a child row: a foreign key constraint fails"与 Zend Framework 1 和 MySQL

php - 如何使用 PHP 从数据库中检索数据?

PHP undefined variable ?

javascript - 为主菜单中所有 'a'标签添加点击事件

javascript - 具有嵌套属性的 HTML SetAttribute