JavaScript clearTimeout无法正确触发

时间:2017-09-15 11:23:01

标签: javascript jquery timer

我在带有JavaScript的文本框上有一个提交功能。当脚本触发时,它会检查某个文章的Kendo网格,并为其数量添加+1,并在编辑模式下打开相应的单元格。我想要实现的是,在每次提交时,启动grid.editCell()的计时器将重置其计时器。

目前,该事件正常发生。但是,虽然clearTimeout()只是启动计时器,然后在之后立即清除,但计时器不会重置。

JavaScript的:

$('#txtBarcode').submit(function (e) {        
    var grid = $("#PickListDetailGrid").data("kendoGrid");
    var dataSource = $("#PickListDetailGrid").data("kendoGrid").dataSource;
    var allData = grid.dataSource.data();
    var code = this.value;
    var notification = $("#notification").data("kendoNotification");
    var timer = null;
    clearTimeout(timer);

    $.each(allData, function (index, item) {
        if (item.ArticleID == code) {
            clearTimeout(timer);
            timer = null;
            if (item.Quantity > item.PickedQuantity && item.PickedQuantity.toString().length < 4) {

                var edit = function () {
                    if (item.PickedQuantity != item.Quantity && timer != null) {
                        grid.select("tr:eq(" + (index) + ")");
                        grid.editCell("tr:eq(" + (index + 1) + ") td:eq(" + (5) + ")");  
                    } else {
                        //do nothing
                    }                           
                }   

                item.PickedQuantity++;
                item.dirty = true;
                dataSource.sync();

                if (item.PickedQuantity != item.Quantity) {
                    console.log("tik tok");                        
                    if (timer) {
                        clearTimeout(timer); //cancel the previous timer.
                        timer = null;
                    }
                    timer = setTimeout(edit, 3000);
                } else {
                    clearTimeout(timer); 
                    timer = null;
                }
                document.getElementById("txtBarcode").value = "";

            } else {    
                if (item.PickedQuantity.toString().length > 4) {

                    notification.hide();
                    notification.show({
                        message: "Added item"
                    }, "upload-success");
                } else {
                    notification.hide();
                    notification.show({
                        title: "Quantity Error",
                        message: "You already picked this item to the maximum"
                    }, "error");
                    document.getElementById("txtBarcode").value = "";
                    grid.select("tr:eq(" + (index) + ")");
                    grid.editCell("tr:eq(" + (index + 1) + ") td:eq(" + (5) + ")");
                    $('.focus :input').focus();
                }
            }
        }
    })
})

2 个答案:

答案 0 :(得分:0)

您可以尝试这样的延迟功能。延迟功能应该在每个功能之外。

var delay = (function() {
   var timer = 0;
   return function(callback, ms) {
       clearTimeout(timer);
       timer = setTimeout(callback, ms);
   };
})();

delay(function() {
   //do something
}, 3000);

答案 1 :(得分:0)

问题是var timer = null;必须在提交函数之外才能正确清除,并在为其分配新的setTimeout()之前设置为null。