gpt4 book ai didi

javascript - 如何在两个不同的函数之间共享变量?

转载 作者:行者123 更新时间:2023-12-01 01:16:18 30 4
gpt4 key购买 nike

我有 2 个函数,对于 PC 来说称为 click ,对于移动语句来说称为 swipe 。每次激活这些函数时,变量 myCount 就会增加。但问题是,当我单击移动报表上的按钮时,myCount 会增加一倍。

How do i pass variables between functions in javascript

根据上面的链接,我尝试使用嵌套函数传递变量,如下所示:

<!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>

但是如果你想启动istouched功能,必须先点击按钮。之后,myCount 再次增加一倍。

这就是我到目前为止的情况:

<!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>

我的期望是获得两个不同函数之间的增量值myCount,并存储该值以供下一个增量使用。因此,如果您点击函数 a 3 次,滑动函数 b 2 次,那么 myCount 将是 5,而不是 3 和 2。

有什么解决办法吗?

最佳答案

您可以使用 Event bus pattern 来做到这一点它允许您在多个不同的功能之间共享数据。

HTML

<h2>COUNT: <span data-count>0</span></h2>

<button id="btn1">Push Me 1</button><br><br>

<button id="btn2">Push Me 2</button>



JS事件总线模式

var EventManager = {
subscribe: function(event, fn) {
$(this).bind(event, fn);
},
publish: function(event) {
$(this).trigger(event);
}
};

// Register your custom code which can publish and subscribe to events
EventManager.subscribe("increaseClickSwipeValue", function() {
$('span[data-count]').text(Counter.increaseValue());
// do something other
});

// counter object which hold value
var Counter = {
value: 0,

increaseValue: function() {
this.value = this.value+1;
return this.value;
}
};

// listeners anywhere
$('#btn1').on('click', function() {
EventManager.publish("increaseClickSwipeValue");
});

$('#btn2').on('click', function() {
EventManager.publish("increaseClickSwipeValue");
});

JSFIDDLE

More about event bus

关于javascript - 如何在两个不同的函数之间共享变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54737110/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com