My javascript variable does not exceed the value of 1

1

I have a javascript code (below) where a variable with the initial value of 0 is declared.

I want to touch the div, its value is set to 1, and to play a second time, its value is set to 2.

The problem is that when I play the div for the first time it adds 1, but when I do it a second time, it does not keep increasing.

This is the code:

<div id="taptoread"  
     ontouchstart="if(touch1403171021t < 2){ touch1403171021t++; }" 
     ontouchmove="if(touch1403171021t > 0){ touch1403171021t--; }" 
     ontouchend="if(touch1403171021t == 2){alert('ee')}; 
                 if(touch1403171021t == 1){ 
                     $('#taptoread').css({'opacity':'1'}); 
                     $('.1403171021').slideDown(170);}" 
     style="opacity:0.7;border-radius:7px;padding-
     bottom:7px;background-color:lightyellow;margin:0px 7px 0px 7px;" 
></div>
    
asked by Kevin Antolinez Miranda 17.03.2017 в 17:06
source

1 answer

1

I would do it this way I create an object like this:

var myObj = {
        touch : 0,

        inc : function() {
            this.touch++;
            console.log(this.touch);
        },

        dec : function () {
            if (this.touch > 0) {
                this.touch--;
            }
            console.log(this.touch);

        },

        action : function () {
            console.log(this.touch);
            if (this.touch === 2) {
                alert('ee')
            } else if (this.touch === 1){
                 $('#taptoread').css({'opacity':'1'}); 
                 $('.1403171021').slideDown(170);
            }
        }

}

and the div:

<div id="taptoread"  
     ontouchstart="myObj.inc();" 
     ontouchmove="myObj.dec();" 
     ontouchend="myObj.action()" 
     style=" width: 100px; height: 100px; opacity:0.7;border-radius:7px;padding-
     bottom:7px;background-color:lightyellow;margin:0px 7px 0px 7px;" 
></div>

in the browser console you will notice that the variable touch does increase

    
answered by 17.03.2017 / 18:40
source