0

i wanted to fire off get_convo after 5000 seconds but it fires automatic after i click a message.

    $(".mes").click(function(){
        var user = $(this).attr("id");
        $("#convo").html(user);
        $(".convo_mes").html("Loading conversation <img width='15' height='15' src='./img/load.gif'>");
        setTimeout(get_convo(user),5000);
    });

    //get convo
    function get_convo(user){
        $.post("./php/get_convo.php",{username:user},function(get_convo){
            $(".convo_mes").html(get_convo);
        });
    }

2 Answers2

1

The function will be invoked as soon as it reads the setTimeout function. You have to give a proper function. So that setTimeout can call that function later. This will be the solution:

$(".mes").click(function(){
        var user = $(this).attr("id");
        $("#convo").html(user);
        $(".convo_mes").html("Loading conversation <img width='15' height='15' src='./img/load.gif'>");
        setTimeout(function(){ get_convo(user)},5000);
    });
Aju John
  • 2,214
  • 1
  • 10
  • 27
1
setTimeout(function() {get_convo(user)}, 5000);

If that doesn't work, then it may just be because AJAX does what it wants.

Jordan Lowe
  • 368
  • 1
  • 13