Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Nested Function Calls Problem

    I have a JS calculator and after each calculation the user makes I re-create the displaying Frame from another Frame where I keep the code and keep track of variables. The function makeImage() re-creates the displaying Frame.

    The calculator has a search feature which searches for items from a database for the user to calculate. After the user completes a search, the displaying Frame is re-created with makeImage() to display the results of the search. It basically displays the contents of a variable named searchDisplay.

    A search can take some time, so I got the clever idea to display the text "Searching..." while the computer was working away. It was the easiest code I ever wrote! At the start of the search function I simply inserted:

    searchDisplay="Searching...";
    makeImage();

    Then as the search function runs it re-creates searchDisplay with the search results and then calls makeImage() again to display searchDisplay -- the results of the search.

    Easy code, but it fails. It never displays.

    Funny thing is I put in alert(searchDisplay) after the above two lines of code and when the computer pauses to display the alert message I see "Searching..." displayed in the background. But without the alert it never shows.

    The code is basically:

    function search(){
    searchDisplay="Searching...";
    makeImage();
    ... search and put results in searchDisplay
    makeImage();
    }

    Again, it works perfectly with the alert in there after the first makeImage().

    If the search were very short, I'd think it might be flashing by so quickly I never see it. But a search can take over 5 seconds so that's not the reason.

    Thanks, Peter

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    got a link?
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I put a test version here:

    www.eatwellandexercise.com/cph/index_food.html

    All you have to do is click on search, or maybe enter something like fruit then click on search. It's working with the alert.

    I thought it might be a problem with timing, so I split up the search function. The first part function Search() displays my little message, then it calls the second part function Search2() which completes the search function and displays the results. It didn't help. It still only works with the alert message.

    Thanks!

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm. I'm not sure, but if the alert() fixes it, then you have a timing issue. Try using a setTimeout for 50ms or so.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Hmm, I never liked the leakyness of setTimeout, and the asynchronousy. Try this:
    Code:
    top._=new Date;
    while(new Date-top._<50);
    delete top._;
    It should give you a delay of 50 milliseconds. Edit the number as you see fit.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I tried both methods putting the code right were the alert was and it didn't help. Actually, with Liorean's I got an error message, "Object doesn't support this code."

    Seems funny that it's a timing problem.

    If the search were really fast, it could be. But the page can certainly be re-created and displayed during a long search time. Unless the computer is busy doing the search. That's why I tried splitting the function, so it clearly did 1. re-create the page then 2. search function. Which didn't help. ???

    Come to think of it, all other times I re-create the page I then do nothing. Waiting for the user's next move.

    Hmm. That seems to make it obvious. It doesn't like being told to re-create the page then do something else. Guess I need:

    onPageRe-created="makeImage()";

    kinda like an onLoad. Wish we could just make things up. (Actually, that's what I do all the time and so I'm always fixing stuff! Haha.)

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, we'll try to simplify, then:
    Code:
    var nTime=new Date;
    while(new Date-nTime<50);
    That shouldn't cause any object problems.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Another nutty idea to try... I do this all day long...

    I'll put the call to function Search2() and then end of makeImage() instead at the end of function Search(). So it won't be called immediately. Search2() will be called at the end of makeImage().

    Anyway I'll try this and post back.

    Thanks!

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Nothing has helped except the alert.

    I put the call to continue the search function at the very end of makeImage... even after the document.write but it didn't help. I guess it needs time to complete the document.write.

    I'll play with it another day with a fresh mind. (Though not likely any smarter!)

    Thanks, Peter

  • #10
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Could you not connect the second run of the function to the onload handler of the frame? That should provide you with what you need.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #11
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I just did that... before I got your note, actually.

    Yes, it seems to work.

    I'm getting the display I want then instead of the search continuing I'm getting an error message object expected but I'm sure I can resove that.

    Thanks!

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    My error was leaving off a parent.create but it didn't matter. Even the onLoad doesn't work. (In my previous post I said it did... the error message had the same effect as an alert... stopping the computing causes the display I want to display to display.)

    Ignoring the anomaly that for some reason inserting an alert seems to make things work, I believe I can state the problem. At least for my IE browser.

    The browser simply does not do anything affecting the display until it has finished all computing. Thus I will always fail trying to display a message saying that I am computing. Or, in my case, searching.

    I got another idea. A better one than the original, actually. I use rollovers to play with button images and have a button that is a spinning arrow. So I decided instead of re-creating the display with different text to simply do a button change. I do that all the time without re-creating the display.

    Same problem!

    I put the call to the function to change the button at the start of Search() and it still failed. Next I tried putting it in the code for the button. On onClick I changed to the spinning arrow. Worked great. Then, doing things in baby steps, on onClick it changed to the spinning arrow then called Search().

    Same problem!

    It just won't do the button change till the search computing is done and then the display is all different anyway.Too late.

    Last idea! You can always change the display of an <input type=text easily. Wrong!

    Same problem!

    It won't even do that with computing (searching) code following.

    So I give up... thinking with my little mind that I have worked out the logic and it's really not possible.

    ???

    Thanks for the help with this.


  •  

    Posting Permissions

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