views:

163

answers:

2

I have a DIV Called fArea and when i click the open button it's sets fo=2 but the code still refreshs so i tryed to add if and else and now it is not displying anything.

jQuery Code:

function unique_requestid() {
    var timestamp = Number(new Date()).toString();
    var random = Math.random() * (Math.random() * 100000 * Math.random() );
    var unique = new String();
    unique = timestamp + random;
    return unique;
}

function FriendsContent(id) {
    if(id == 2){
        var refresh = setInterval(
            function() {
                $("#fArea").load("friends_online.php?fo="+id+"&random=" +
                    unique_requestid());
            }, 5000
        );
    } else {
        $("#fArea").load("friends_online.php?fo="+id+"&random=" +
            unique_requestid());
    };
}
+5  A: 

First of all, in the code you pasted you have a missing }. I am not sure if this is a pasting mistake or not. Once we get past that, what the code right now is doing is waiting 5 seconds to make the call if the ID is 2, and making a request every 5 seconds after that, or simply doing a one-time immediate request otherwise. It is hard to tell where this is going wrong without seeing the code that is calling this function.

What you said you want to do, however, sounds like only making the request if the ID is 2, in which case:

function FriendsContent(id) {
    if(id == 2) {
        var refresh = setInterval(function() {
            $("#fArea").load("friends_online.php?fo="+id+"&random=" + unique_requestid());
        }, 5000);
    }
}

At the end of the day, though, if you want proper help on a site like this you have to take care into properly phrasing what you want to do and the steps you have taken thus far to get yourself there.

Paolo Bergantino
Sorry that did not work but i added some more info
Gully
+1  A: 

If nothing is being displayed, make sure that you can access the friends_online.php page directly. Also, try adding a clearInterval() to the else condition to prevent refresh if id==2 is selected and then a different id is picked.

function FriendsContent(id) {
    var refresh;
    if(id == 2){
        refresh = setInterval(
         function() {
            $("#fArea").load("friends_online.php?fo="+id+"&random=" +
                unique_requestid());
          }, 5000
        );
    } else {
        clearInterval(refresh);
        $("#fArea").load("friends_online.php?fo="+id+"&random=" +
            unique_requestid());
    };
}
Jose Basilio