数组存储引用变量不工作-Javascript

数组存储引用变量不工作-Javascript,javascript,arrays,variables,reference,Javascript,Arrays,Variables,Reference,我是一个JavaScript新手,所以投身于新事物会让我遇到很多问题 在这种情况下,我想存储一个引用变量数组,但它不起作用 var pressTimer, timers = []; function addEventTapHold(element, func, time) { document.getElementById(element).addEventListener('touchend', function(){ console.l

我是一个JavaScript新手,所以投身于新事物会让我遇到很多问题

在这种情况下,我想存储一个引用变量数组,但它不起作用

    var pressTimer, timers = [];
    function addEventTapHold(element, func, time) {
        document.getElementById(element).addEventListener('touchend', function(){
            console.log("Clear allll");
            //TRY TO CLEAR TIMEOUT OF ARRAY BUT NOT WORK
            for (var timer in timers) {
                clearTimeout(timer);
            }
            timers = [];

            //IMPLEMENT CODE


            return;
        });

        document.getElementById(element).addEventListener('touchstart', function(event){
            pressTimer = setTimeout(function(){
                func(element);
                isTapHold = true;
            },time);

            //PUSH TIMER TO ARRAY 
            timers.push(pressTimer);
        });
    }
我期待着很快听到一些建议。
非常感谢。

看起来您的函数需要的是参数“time”,而不是“timers”

旁注:不要在循环中使用
for..来迭代数组。不是:“pressTimer=setTimeout(”,但是,它是:“var pressTimer=setTimeout(”。@FelixKling:我试图调试,并且数组存储引用变量是正确的,但是当我使用它来清除Timeout时,它仍然不起作用。@MattBall:我只想清除所有的timer,所以我认为如果数组中元素的顺序不正确,它是可以的。顺便说一下,我使用了“常规”语句和问题仍然存在。您能有其他建议吗?哦,抱歉,这是我的错误。应该更清楚,“timers”声明为函数外,“time”传递变量以设置超时。我编辑了说明。
    var timers = [];
    function addEventTapHold(element, func, time) {
        document.getElementById(element).addEventListener('touchend', function(){
            console.log("Clear allll");
            //TRY TO CLEAR TIMEOUT OF ARRAY BUT NOT WORK
            for (var timer in timers) {
                clearTimeout(timer);
            }
            timers = [];

        //IMPLEMENT CODE


        return;
    });


    document.getElementById(element).addEventListener('touchstart', function(event){

         //define the pressTimer as local variable here 
         var pressTimer = setTimeout(function(){
            func(element);
            isTapHold = true;
        },time);

        //PUSH TIMER TO ARRAY 
        timers.push(pressTimer);
    });
}