如何在按下按钮并打洞时连续调用函数

How to continuously call a function when button is pressed and holed?

本文关键字:连续 调用 函数 按钮      更新时间:2023-09-26

当用户连续按下按钮或按住按钮时,我想连续调用一个函数。我试过,但功能调用只有两次,当用户按下和释放按钮时。我试过这个:

zoomIn.on("mousedown", function (e) {
            setTimeout(function() {
                ImageViewer.zoomImage(30, true);
            }, 300);
        });

这不能正常工作。我有办法做到这一点吗?

您可以尝试以下方法:

var interval,
    time = 100;
var zoom = function() {
    //your zooming code:
    ImageViewer.zoomImage(30, true);
};
var startZoom = function() {
    interval = setInterval( zoom, time );
};
var stopZoom = function() {
    clearInterval( interval );
};
zoomIn.on("mousedown", startZoom);
zoomIn.on("mouseup", stopZoom);