点击Javascript SetTimeOut时不考虑2-3次点击后的时间延迟

时间:2019-07-08 14:21:26

标签: javascript dhtmlx

我正在用javascript dhtmlx编写代码。我在网格上有一个onclick事件。当我单击网格时,出现一个窗口,我想在4秒后消失。 我尝试了setTimeOut,但是连续2-3次单击后,它弄乱了时间,几乎立即隐藏了窗口。这是代码:

 var dhxWindow = new dhx.Window({
                    modal: false,
                    header: false,
                    footer: false,
                    resizable: false,
                    css: "toolbar",
                    movable: false,
                    height: 130,
                    expire: 4000
                });

var toolbar = new dhx.Toolbar("toolbar_container", {

                });
grid.events.on("CellClick", function (row, column, e) {

                    var data = [
                        {
                            id: "data1",
                            type: "imageButton",


                        },
                        {
                            id: "data2",
                            type: "imageButton",

                        },
                        {
                            id: "data3",
                            type: "imageButton",

                        }

                    ];

                    toolbar.data.parse(data);
                    dhxWindow.attach(toolbar);
                    dhxWindow.show();

                    setTimeout(function () {
                        dhxWindow.hide();
                    }, 4000);


                });

我在做什么错?谢谢

1 个答案:

答案 0 :(得分:3)

var myTimeout;
grid.events.on("CellClick", function (row, column, e) {
                    if(myTimeout) clearTimeout(myTimeout); // cancel timeout if present
                    var data = [
                        {
                            id: "data1",
                            type: "imageButton",


                        },
                        {
                            id: "data2",
                            type: "imageButton",

                        },
                        {
                            id: "data3",
                            type: "imageButton",

                        }

                    ];

                    toolbar.data.parse(data);
                    dhxWindow.attach(toolbar);
                    dhxWindow.show();





                    myTimeout =  setTimeout(function () {
                        dhxWindow.hide();
                    }, 4000); // Reinstantiate timeout



                });