簡體   English   中英

將參數從一個函數傳遞到另一個函數

[英]Pass arguments from one function to another

在這里我一直在開發一個chat應用程序。 這里我想根據用戶的email address加載chat messages 在這里,我想將email address傳遞email address后端。

javascript ,我想將電子郵件從(".friend").each(function(x){傳遞給LoadChat函數。它顯示在LoadChat函數的console.log中,立即變為undefined

這是Javascript代碼

     $(".friend").each(function(x){     
          $(this).click(function(){
                        
              var name = $(this).find("p strong").html();
              var email = $(this).find("p span").html();
              var id = $(this).find("p spans").html();
                    
              LoadChat(email);
                                                
              $("#profile p").html(name);
              $("#profile span").html(email);           
              
              $(".message").not(".right").find("img").attr("src", $(clone).attr("src"));                                    
              $('#friendslist').fadeOut();
              $('#chatview').fadeIn();
                 
          setTimeout(function(){
          $('#chatview').fadeOut();
          $('#friendslist').fadeIn();               
       }, 50);            
    });              
   });
}); 

這是另一個LoadChat函數

<script type="text/javascript">
    LoadChat();
    
    setInterval(function () {
        LoadChat();
    }, 1000);

    function LoadChat(email) {
        
console.log(email);
$.post('handlers/messages.php?action=getMessages&email='+email, function (response) {

    var scrollpos = $('#chat-messages').scrollTop();
    var scrollpos = parseInt(scrollpos) + 420;
    var scrollHeight = $('#chat-messages').prop('scrollHeight');

    $('#chat-messages').html(response);
    if (scrollpos < scrollHeight){

    } else{
        $('#chat-messages').scrollTop($('#chat-messages').prop('scrollHeight'));
    }

})

}

    $('form').submit(function () {
        //alert('form is submit jquery');
        var message = $('.textarea').val();

        if ($(".textarea").val()) {

        $.post('handlers/messages.php?action=sendMessage&message='+message, function (response) {
            //alert(response);
            if (response==1){
                LoadChat();
                document.getElementById('msgform').reset();
            }
        });

    } else {

        myFunction();

}
        return false;

    })
</script>

任何幫助可能會受到高度贊賞

您可以在幾個地方不帶參數調用LoadChat() 調用不帶參數的函數會將其參數設置為undefined如果有)。 如果要設置email ,請將其作為函數調用的參數提供。

 function LoadChat(email) { console.log(email); } // call without arguments LoadChat(); // call with arguments LoadChat("john.doe@example.com");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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