I have 2 functions that are called click
for PC and swipe
for Mobile statement. Everytime you active those functions, a variable myCount
increases. But the problem is myCount
increases double when I click the button on mobile statement.
How do i pass variables between functions in javascript
According to the above link, I've tried to pass the variable by using nested function something like this:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<style media="screen" ref="">
* {
margin: 0;
padding: 0;
}
.page1 {
position: relative;
width: 100vw;
height: 100vh;
background-color: grey;
display: flex;
flex-flow: row;
justify-content: center;
align-items: center;
}
.buttons {
display: flex;
flex-flow: row;
justify-content: center;
align-items: center;
}
.prev {
position: absolute;
left: 0;
margin: 0 40px;
}
.shrink {
position: absolute;
flex-grow: 1.5;
}
.next {
position: absolute;
right: 0;
margin: 0 40px;
}
</style>
</head>
<body>
<div class="page1">
<div class="buttons">
<div class="button prev">Prev</div>
<div class="shrink"></div>
<div class="button next">Next</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function stripeAnimeModule() {
// Variables (for Click)
var _ = $('#stripe > .container > .grid > .inline-grid'),
screen = $('.page1'),
buttons = $('.page1 > .buttons > .button'),
myCount = 1,
x,
dist;
// functions
function isclicked() {
buttons.on({click: clicked})
function clicked(e) {
myCount += 1;
istouched(myCount);
}
}
function istouched(p) {
screen.bind({touchstart: touchStart, touchmove: touchMove, touchend: touchEnd})
console.log(p);
function touchStart(e) {
return x = e.touches[0].clientX;
console.log(p);
}
function touchMove(e) {
var drag = e.touches[0].clientX;
var dist = Math.sqrt(x + drag);
e.preventDefault();
}
function touchEnd(e) {
var dist = e.changedTouches[0].clientX - x;
console.log('basic log: ' + dist, myCount);
}
}
isclicked();
})
</script>
</body>
</html>
But this one must have to click the buttons first if you want to start istouched
function. After that, myCount
increases double again.
This is how I've got so far:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<style media="screen" ref="">
* {
margin: 0;
padding: 0;
}
.page1 {
position: relative;
width: 100vw;
height: 100vh;
background-color: grey;
display: flex;
flex-flow: row;
justify-content: center;
align-items: center;
}
.buttons {
display: flex;
flex-flow: row;
justify-content: center;
align-items: center;
}
.prev {
position: absolute;
left: 0;
margin: 0 40px;
}
.shrink {
position: absolute;
flex-grow: 1.5;
}
.next {
position: absolute;
right: 0;
margin: 0 40px;
}
</style>
</head>
<body>
<div class="page1">
<div class="buttons">
<div class="button prev">Prev</div>
<div class="shrink"></div>
<div class="button next">Next</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function stripeAnimeModule() {
// Variables
var screen = $('.page1'),
buttons = $('.page1 > .buttons').find('button'),
myCount = 1,
x,
dist;
// functions
$(function istouched() {
screen.bind({click: clicked, touchstart: touchStart, touchmove: touchMove, touchend: touchEnd})
function clicked(e) {
if (buttons.data('clicked', true)) {
myCount += 1;
console.log(myCount);
}
}
function touchStart(e) {
return x = e.touches[0].clientX;
}
function touchMove(e) {
var drag = e.touches[0].clientX;
var dist = Math.sqrt(x + drag);
e.preventDefault();
console.log(x, drag);
}
function touchEnd(e) {
var dist = e.changedTouches[0].clientX - x;
myCount += 1;
console.log('distance is: ' + dist, 'myCount is: '+ myCount);
}
})
})
</script>
</body>
</html>
My expectation is to get a increment value myCount
between 2 different functions, and store that value for next increment. So it goes like if you clicked function a
3rd times, swiped function b
2nd times then myCount
would be 5, not 3 and 2.
Any solutions?