检查特定div中的touchend是否为真

时间:2014-10-15 13:13:49

标签: javascript jquery html css touch

我有6个DIV。它们中的每一个都有相应的touchstarttouchend代码块。但是,如果我全部触摸它们并立即释放它们,则所有DIV的touchend事件都会触发。我想要的是,如果至少有一个DIV在其中发射touchend事件,则其他DIV的另一个touchend事件将不会触发。如果你需要,我可以给你我的代码。我是Web Technologies的新手

修改

这是我的代码。对不起它有点重复。

var array = [0,1,2,3,4,5];
var ktr = 6;
var apple01Show,apple02Show,apple03Show,lemon01Show,lemon02Show,lemon03Show;
var apple01Release,apple02Release,apple03Release,lemon01Release,lemon02Release,lemon03Release;

function startGame(){
        clearTimeout(apple01Show);
        clearTimeout(apple02Show);
        clearTimeout(apple03Show);
        clearTimeout(lemon01Show);
        clearTimeout(lemon02Show);
        clearTimeout(lemon03Show);
        clearTimeout(apple01Release);
        clearTimeout(apple02Release);
        clearTimeout(apple03Release);
        clearTimeout(lemon01Release);
        clearTimeout(lemon01Release);
        clearTimeout(lemon01Release);
        console.log(array.length +' array length');
        if(ktr < 1){
            //activate div of logo
            $('#logo').show();
            setTimeout(function (){
                restart();
                disableAllTouches();
                startGame();
                array = [0,1,2,3,4,5]; //, 2, 3, 4, 5, 6, 7, 8, 9
                ktr = 6;
                $('#logo').hide();
            },1000);
        }else

        shuffle(array);
        if(typeof array[0] == 'undefined'){
            //activate div of logo
            $('#logo').show();
            setTimeout(function (){
                restart();
                disableAllTouches();
                startGame();
                array = [0,1,2,3,4,5]; //, 2, 3, 4, 5, 6, 7, 8, 9
                ktr = 6;
                $('#logo').hide();
            },1000);
        }
        console.log(array);
        console.log(array[0]);

        switch(array[0]){
        case 0: //greenApple01
            console.log('greenApple01');

            apple01Show = setTimeout(function () {
                $('#apple01').show();
            },200)
            $('#greenApple01').show();

            console.log('randomnumber: '+array[0] +' greenApple01');
            $("#apple01").on('touchstart', function(){
                console.log(ktr+' ktr');
                console.log(array);
                console.log('value of array at greenApple01: '+array);
                array.splice(0,1);
                startGame();
                ktr--;
            }).on('touchend', function(){
                apple01Release = setTimeout(function (){
                    console.log('released greenApple01');
                    restart(); //hide all elements
                    disableAllTouches();
                    array.length = 0;
                    array = [0,1,2,3,4,5];
                    ktr = 6;
                    startGame(); //spawn them again
                },1000);
            });
            break;
        case 1: // greenApple02
            console.log('greenApple02');

            apple02Show = setTimeout(function () {
                $('#apple02').show();
            },200);
            //$('#apple01').show();
            $('#greenApple02').show();

            console.log('randomnumber: '+array[0] +' greenApple01');
            $("#apple02").on('touchstart', function(){
                console.log(ktr+' ktr');
                console.log(array);
                array.splice(0,1);
                console.log('value of array at greenApple02: '+array);
                startGame();
                ktr--;
            }).on('touchend', function(){
                apple02Release = setTimeout(function (){
                    console.log('released greenApple02');
                    restart();
                    disableAllTouches();
                    array.length = 0;
                    array = [0,1,2,3,4,5];
                    ktr = 6;
                    startGame();
                },1000);
            });
            break;
        case 2: // greenLemon01
            console.log('greenLemon01');

            lemon01Show = setTimeout(function () {
                $('#lemon01').show();
            },200);
            $('#greenLemon01').show();
            console.log('randomnumber: '+array[0] +' greenLemon01');
            $("#lemon01").on('touchstart', function(){

                console.log(ktr+' ktr');
                console.log(array);
                array.splice(0,1);
                console.log('value of array at greenLemon01: '+array);
                startGame();
                ktr--;
            }).on('touchend', function(){
                lemon01Release = setTimeout(function (){
                    console.log('released greenLemon01');
                    restart();
                    disableAllTouches();
                    array.length = 0;
                    array = [0,1,2,3,4,5];
                    ktr = 6;
                    startGame();
                },1000);
            });
            break;
        case 3: // greenLemon02
            console.log('greenLemon02');
            lemon02Show = setTimeout(function () {
                $('#lemon02').show();
            },200);
            $('#greenLemon02').show();
            console.log('randomnumber: '+array[0] +' greenLemon02');
            $("#lemon02").on('touchstart', function(){
                console.log(ktr+' ktr');
                console.log(array);
                array.splice(0,1);
                console.log('value of array at greenLemon02: '+array);
                startGame();
                ktr--;
            }).on('touchend', function(){
                lemon02Release = setTimeout(function (){
                    console.log('released greenLemon02');
                    restart();
                    disableAllTouches();
                    array.length = 0;
                    array = [0,1,2,3,4,5];
                    ktr = 6;
                    startGame();
                },1000);
            });
            break;
        case 4: // redLemon02
            console.log('redLemon02');
            lemon03Show = setTimeout(function () {
                $('.lemon03').show();
            },200);
            $('#redLemon02').show();
            console.log('randomnumber: '+array[0] +' redLemon02');
            $(".lemon03").on('touchstart', function(){
                console.log(ktr+' ktr');
                console.log(array);
                array.splice(0,1);
                console.log('value of array at redLemon02: '+array);
                startGame();
                ktr--;
            }).on('touchend', function(){
                lemon03Release = setTimeout(function (){
                    console.log('released redLemon02');
                    restart();
                    disableAllTouches();
                    array.length = 0;
                    array = [0,1,2,3,4,5];
                    ktr = 6;
                    startGame();
                },1000);
            });
            break;
        case 5: // yellowApple03
            console.log('yellowApple03');
            apple03Show = setTimeout(function () {
                $('#apple03').show();
            },200);
            $('#yellowApple03').show();
            console.log('randomnumber: '+array[0] +' yellowApple03');
            $("#apple03").on('touchstart', function(){
                console.log(ktr+' ktr');
                console.log(array);
                array.splice(0,1);
                console.log('value of array at yellowApple03: '+array);
                startGame();
                ktr--;
            }).on('touchend', function(){
                apple03Release = setTimeout(function (){
                    console.log('released yellowApple03');
                    restart();
                    disableAllTouches();
                    array.length = 0;
                    array = [0,1,2,3,4,5];
                    ktr = 6;
                    startGame();
                },1000);
            });
            break;
        default:
            break;
    }

    }

我的三个功能:功能1:

function shuffle(array) {
          var currentIndex = array.length, temporaryValue, randomIndex ;

          // While there remain elements to shuffle...
          while (0 !== currentIndex) {

            // Pick a remaining element...
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;

            // And swap it with the current element.
            temporaryValue = array[currentIndex];
            array[currentIndex] = array[randomIndex];
            array[randomIndex] = temporaryValue;
          }

          return array;
    }

功能2:

function restart(){
        $("#greenApple01").hide();
        $("#greenApple02").hide();
        $("#greenLemon01").hide();
        $("#greenLemon02").hide();
        $("#redLemon02").hide();
        $("#yellowApple03").hide();
        $('#apple01').hide();
        $('#apple02').hide();
        $('#apple03').hide();
        $('#lemon01').hide();
        $('#lemon02').hide();
        $('#lemon03').hide();
        $("#logo").hide();
    }

功能3:

function disableAllTouches(){
        $('#apple01').off('touchstart');
        $('#apple02').off('touchstart');
        $('#apple03').off('touchstart');
        $('#lemon01').off('touchstart');
        $('#lemon02').off('touchstart');
        $('.lemon03').off('touchstart');
    }

0 个答案:

没有答案