Page 1 of 4 123 ... LastLast
Results 1 to 10 of 33

Thread: Calling Functions from Functions.

  1. #1
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Calling Functions from Functions.

    I am playing around with Ajax in a chat script I am making, and have encountered some loading problems.

    I have a function Init() which initiates on load.

    This runs getMessages(); and sets an interval of 2 seconds, to continue running it. This gets the messages from the database.

    I want to also get details of who is online at a given time from a separate database. I have done the exact same again. I run getOnline(), and then set it to run every 2 seconds using setInterval();.

    When I introduce the getOnline();, getMessages(); no longer works... no messages are displayed on the page. As soon as I remove it, it is fine. Likewise if I remove getMessages(); getOnline(); works fine.

    Why is this?

    I attempted another idea. I had:

    Code:
    setInterval('getMessages()', 2000);
    getMessages();
    then rather than do the same for getOnline();, I called the function getOnline(); from getMessages();

    I.E getMessages is run every 2 seconds, and when called it also runs getOnline();

    Once again getOnline() works.. I can see who is online, but I cannot see any messages.

    Have I missed something here, or am I doing something wrong.


    This is not a code problem: the code works fine, simply they do not work together. Have I missed some intrinsic rule about how things can be called?

    Cheers

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    When you put something in quotes, then it is not code any more-- it is a string.

    function() is a function. 'function()' is just characters-- text.

    The first line you posted above won't work.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,947
    Thanks
    43
    Thanked 3,193 Times in 3,156 Posts
    Blog Entries
    12

    Default

    In a timeout or interval quotes are valid syntax for function execution, though not preferred. Without seeing the code involved in these two client side functions it would be hard to say for sure, but it sounds like there is a conflict between them, either on the client side, the server side, or both.

    Please post a link to the page on your site that contains the problematic code so we can check it out.


    That will help in large measure in determining if there is a conflict on the client side, perhaps even resolve this if that is the only issue. If the problem is on the server side, it will at best provide clues, perhaps nothing at all. For debugging the server side we would need to see the server side code involved. That cannot be determined from a link to the page.
    Last edited by jscheuer1; 12-16-2009 at 05:45 AM. Reason: usage
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    djr33 (12-16-2009)

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    When quotes are used, is the syntax with () also used? In php there are a few functions that use string-names of functions to call them, but they don't use "function()", instead: "function". But, yes, getting off topic. Thanks for clearing that up.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,947
    Thanks
    43
    Thanked 3,193 Times in 3,156 Posts
    Blog Entries
    12

    Default

    In javascript it is the opposite of that:

    Code:
    onload = function(){
    	setTimeout("alert('Cowabunga!')", 3000);
    };
    and:

    Code:
    function cow(){
    	alert('Cowabunga!');
    }
    onload = function(){
    	setTimeout("cow()", 3000);
    };
    will work, but it is not encouraged because it uses the eval engine to turn the string into code. Errors can often creep in with that, and nested quotes can become confusing to the eye.

    This will also work:

    Code:
    function cow(){
    	alert('Cowabunga!');
    }
    onload = function(){
    	setTimeout(cow, 3000);
    };
    No quotes, and no (). But it fires with an event for cow, which can become confusing if cow can at times accept parameters. So the preferred method is:

    Code:
    function cow(){
    	alert('Cowabunga!');
    }
    onload = function(){
    	setTimeout(function(){cow();}, 3000);
    };
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. The Following User Says Thank You to jscheuer1 For This Useful Post:

    djr33 (12-16-2009)

  8. #6
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Interesting posts, but not too useful for this particular problem.

    I would post code if it were a code specific question. I will simplify it.

    I have 4 functions.

    checkWhoIsWriting();
    getMessages();
    checkRooms();
    getRoomName();

    Independent of what they do (although it is pretty obvious).

    Basically If i use setInterval, or setTimeout to run any ONE of these functions every x seconds, it works fine. If in any capacity I try and have them all run every w,x,y, and z seconds respectively... none work.. the script goes sluggish, things that are meant to happen simply dont etc.

    The general question is why with Ajax calling PHP scripts do multiple setInterval/setTimeout calls not work, and what is the workaround?
    Thanks

  9. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,947
    Thanks
    43
    Thanked 3,193 Times in 3,156 Posts
    Blog Entries
    12

    Default

    They probably conflict, we need to see the code.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #8
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Code:
    function whoiswriting(){
    //document.getElementById("state").innerHTML = document.chatform.guestname.value + " is Writing....";
    
    req.open("GET", "writing.php?page=who");
    req.onreadystatechange = handlewhowritResp;
    req.send(null);
    
    setTimeout('whoiswriting()', 1500);
    }
    
    function handlewhowritResp(){
    if(req.readyState == 4 && req.status == 200){
    document.getElementById("state").innerHTML = req.responseText;
    //document.chatform.chatresp.value = req.responseText;
    //var txt = document.createTextNode(req.responseText+"\n----------------------------------------\n");
    //document.chatform.chatresp.appendChild(txt);
    var elem = document.getElementById("state");
    elem.scrollTop = elem.scrollHeight;
    }
    }
    That is the whoiswriting() function. The other 3 functions are of the exact same form. I literally copied and pasted the above, changed the php script it calls, and changed the div the response goes to.

    I am not quite sure HOW these could be conflicting...
    Cheers

  11. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,947
    Thanks
    43
    Thanked 3,193 Times in 3,156 Posts
    Blog Entries
    12

    Default

    The req object looks to be global. It could be overwriting the same named ones from the other function(s). If that's what's happening, whichever one fires last will be the dominant one. It may not be easy to tell which one is firing last though.

    To tell any more I would probably need a link to the page.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  12. #10
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the useful response John.

    I am new to this, and have been copying/pasting from all over the place.

    The start of the script contains:

    PHP Code:
    function makeRequest(){
    if(
    window.XMLHttpRequest){
    return new 
    XMLHttpRequest();
    } else if(
    window.ActiveXObject){
    return new 
    ActiveXObject("Microsoft.XMLHTTP");
    } else {
    err("You browser doesn't support XMLHttpRequestObject");
    }
    }
    var 
    req makeRequest(); 
    Now I have a basic understanding of what this does in terms of what type of request to do dependent on browser.....

    After your post, I changed the latter bit to:

    PHP Code:
    var req makeRequest();
    var 
    aeq makeRequest();
    var 
    beq makeRequest();
    var 
    ceq makeRequest();
    var 
    deq makeRequest(); 
    I then used a different one for each function, and although not perfect... it is working significantly better.

    Could you elaborate on why one might (and apparently was) overwriting the others, as I was under the impression that the first code snippet simply said 'we are using ajax' for any and all statements.

    Thanks a lot !

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •