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

时间:2014-05-10 02:24:00

标签: javascript arrays variables reference

我是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);
        });
    }

我期待很快听到一些建议。 非常感谢你。

2 个答案:

答案 0 :(得分:0)

看起来你的函数期待参数“time”,而不是“timers”

答案 1 :(得分:0)

    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);
    });
}