簡體   English   中英

Javascript將變量值​​從一個函數傳遞給另一個函數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM