Skip to content Skip to sidebar Skip to footer

Why This Mmediately Invoked Functions Is Not Working Properly

I am using immediately invoked functions pattern, but this is not passing the id 1.When the user clicks on $user, id is passed and the chat window appears echo '
setTimeout(chatcom_load_one(id), 1000);

Here's an updated version of your code with these errors fixed:

functionchat_com_one(id) {

    $('#chatcom').show('fast');
    (functionchatcom_load_one(id) {
        $.post('sendchat2.php', {
            option: 'chatcom_load_one',
            tocom: id
        }, function (data) {
            $('#chatcom #commid #commidwin').html(data);
            setTimeout(function () {
                chatcom_load_one(id);
            }, 1000);
        });
    }());

    $('#chatcom_send').click(function () {
        var text = document.getElementById('chatcom_text').value;
        $.post('sendchat2.php', {
            option: 'chat_com_send_one',
            text: text,
            tocom: id
        },
        function (data) {
            document.getElementById('chatcom_text').value = '';
        });
    });
}

Also, since you're using jQuery, you can simplify document.getElementById.... Another updated version (with some more changes to make it more readable):

functionchat_com_one(id) {
    $('#chatcom').show('fast');

    (functionchatcom_load_one(id) {
        $.post('sendchat2.php', {
            option: 'chatcom_load_one',
            tocom: id
        }, function (data) {
            $('#commidwin').html(data);
            setTimeout(function () {
                chatcom_load_one(id);
            }, 1000);
        });
    }(id));

    $('#chatcom_send').click(function () {
        var text = $('#chatcom_text').val();
        $.post('sendchat2.php', {
            option: 'chat_com_send_one',
            text: text,
            tocom: id
        },
        function (data) {
            $('#chatcom_text').val('');
        });
    });
}

These are just a few cleanups, there may be others.

Edit:

Added devnull69's insight to my final updated code. Hopefully it's useful (accept his answer if this was the problem).

Edit: Other notes

Why are you doing $.post in chatcom_load_one? It would make much more sense as a $.get, and the query parameters would still be sent. It's not really a problem per se, but it's bad style. This should probably be in a file called getchat.php or something instead of doing what I expect is looking for the text parameter.

Also, I don't know the implementation of sendchat2.php, but you should probably reduce the timeout. Try something like 250ms or so. That's not going to overload the server and it'll improve response time.

Solution 2:

In addition to the previously mentioned problems you invoke the immediately executed function without any parameter. You will have to add the (id) parameter to the function call in order to hand over the id element to its local copy inside the function.

(function chatcom_load_one(id) {
   ...
}(id));

Post a Comment for "Why This Mmediately Invoked Functions Is Not Working Properly"