[英]Javascript pass variable value from one function to another
每当我调用check_scroll()
函数时,都需要将值7作为参数传递。
但是现在,即使我调用check_scroll()
函数, lastcount
值也继续增加。
请给我一些建议。
就像我说的那样检查代码片段,
首先单击第first click me to initiate check_scroll function
按钮,然后在div
内滚动,您会收到一个警报,其值增加7。
然后单击按钮,然后再次滚动,但是现在警报不会从7开始。
$("#mybutton").click(function() { check_scroll(7); }) function check_scroll(val) { var lastcount = val; $('#notification_ul').scroll(function() { if ($('#notification_ul').scrollTop() + $('#notification_ul').innerHeight() == $('#notification_ul')[0].scrollHeight) { $("#notification_ul").append("<br/> Some Text Append <br/>"); alert(lastcount); lastcount = lastcount + 7; } }) }
div { width: 200px; height: 200px; overflow: auto; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="notification_ul"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </div> <button id="mybutton"> first click me to initiate check_scroll function </button>
scroll
事件 lastcount
移到外面 lastcount
$(function(){
var lastcount = 0;
$("#mybutton").click(function() {
check_scroll(7);
});
$('#notification_ul').scroll(function() {
if (lastcount !== 0) {
if ($('#notification_ul').scrollTop() + $('#notification_ul').innerHeight() == $('#notification_ul')[0].scrollHeight) {
$("#notification_ul").append("<br/> Some Text Append <br/>");
console.log(lastcount);
lastcount = lastcount + 7;
}
}
});
});
function check_scroll(val) {
lastcount = val;
}
提出您的建议后的最终答案,谢谢你们的快速解决方案和建议。 竖起大拇指
var lastcount = 7; $("#mybutton").click(function() { check_scroll(7); }) $('#notification_ul').scroll(function() { if ($('#notification_ul').scrollTop() + $('#notification_ul').innerHeight() == $('#notification_ul')[0].scrollHeight) { $("#notification_ul").append("<br/> Some Text Append <br/>"); alert("last count after scroll " + lastcount); lastcount = lastcount + 7; } }) function check_scroll(val) { lastcount = val; alert("Last count reseted to " + lastcount); }
div { width: 200px; height: 200px; overflow: auto; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="notification_ul"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </div> <button id="mybutton"> sometext </button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.