Javascript将变量值​​从一个函数传递给另一个函数

时间:2016-06-29 13:37:27

标签: javascript jquery

每当我调用check_scroll()函数时,我都需要将值7作为参数传递。

但是现在即使我调用lastcount函数,check_scroll()值也会继续增加。

请给我一些建议。

像我说的那样检查代码段,

首先点击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;
      }

    })
  }
&#13;
div {
  width: 200px;
  height: 200px;
  overflow: auto;
}
&#13;
<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

  • 仅定义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;
}

答案 1 :(得分:0)

在提到您的建议后的最终答案,谢谢你们快速的解决方案和建议。竖起大拇指

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>