Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php onkeyup太慢了_Php_Javascript_Jquery_Ajax_Onkeyup - Fatal编程技术网

Php onkeyup太慢了

Php onkeyup太慢了,php,javascript,jquery,ajax,onkeyup,Php,Javascript,Jquery,Ajax,Onkeyup,我必须这样写,因为它是动态生成的,下面列出了实际代码。我在jsbin中将它简化了一点。基本上,用框的值更新数组需要很长时间,因此无法使用 谢谢你看 代码: php 您真的要在每个键入的键上过帐还是在用户完成键入时过帐?大多数人可以在处理一个字母之前键入一个完整的单词。你需要数一数。大概是这样的: var count = 0; function doEditItemInCart(newValue,fieldName,itemNum) { count++; setTimeout("e

我必须这样写,因为它是动态生成的,下面列出了实际代码。我在jsbin中将它简化了一点。基本上,用框的值更新数组需要很长时间,因此无法使用

谢谢你看

代码: php


您真的要在每个键入的键上过帐还是在用户完成键入时过帐?大多数人可以在处理一个字母之前键入一个完整的单词。你需要数一数。大概是这样的:

var count = 0;
function doEditItemInCart(newValue,fieldName,itemNum)
{
    count++;
    setTimeout("editItemInCart('"+newValue+"','"+fieldName+"',"+itemNum+","+count+")",200);
}
function editItemInCart(newValue,fieldName,itemNum,cnt) {
if (count == cnt) {
        count = 0;
        jQuery.ajax({
            type:"POST",
            url: "editItem.html",
            data: "newvalue=" + newValue + "&fieldname=" + fieldName + "&itemNum=" + itemNum,
        })
        //alert(newValue + fieldName + itemNum);
    }
}

根据您的评论,听起来您想启动
keyup
事件。我推荐本·阿尔曼的


上面的代码消除了内联事件处理程序,使您能够很好地分离标记和代码。

您希望这能做什么?速度慢的不是keyup事件,而是脚本必须等待服务器响应AJAX查询。您是否测量了editItem.html处理查询所需的时间?我希望它根据那里的两个事件使用新值更新数组。页面不到一秒钟,没问题。我认为在等待不活动然后更新的地方增加一个延迟会更好,所以它不会不断地触发。一个完整的往返HTTP请求需要(比如)1秒。在同一时间段内,我甚至不用尝试就可以写出至少5到6个字符。这意味着您以前的ajax请求将忙于处理现在完全过时的数据,因为它们至少过期了5或6个字符。我想我需要在某个地方放一个“保存”的div,以便客户知道。我真的不想再添加一个库,即使是一个小库。我标记的答案使用了我已有的代码。谢谢你!
function editItemInCart(newValue,fieldName,itemNum) {
    jQuery.ajax({
        type:"POST",
        url: "editItem.html",
        data: "newvalue=" + newValue + "&fieldname=" + fieldName + "&itemNum=" + itemNum,
    })
    //alert(newValue + fieldName + itemNum);
}
var count = 0;
function doEditItemInCart(newValue,fieldName,itemNum)
{
    count++;
    setTimeout("editItemInCart('"+newValue+"','"+fieldName+"',"+itemNum+","+count+")",200);
}
function editItemInCart(newValue,fieldName,itemNum,cnt) {
if (count == cnt) {
        count = 0;
        jQuery.ajax({
            type:"POST",
            url: "editItem.html",
            data: "newvalue=" + newValue + "&fieldname=" + fieldName + "&itemNum=" + itemNum,
        })
        //alert(newValue + fieldName + itemNum);
    }
}
var itemNum = $('#item_num_id').val();
$('#textarea_id').keyup($.debounce(250, editItemInCart(this.value,'comments', itemNum)));