www.webdeveloper.com
Results 1 to 8 of 8

Thread: Testing javascripts

  1. #1
    Join Date
    Jun 2010
    Posts
    37

    Testing javascripts

    When I run javascripts that make big calculations in web browsers, some of them say "the script is stuck in a loop. Do you want to continue?" And I say yes, and it goes on... and after many clicks to continue, it finally prints out a result I want.

    Different browsers do different things. Some just choke, others sort of refuse to continue and even have to be force quit. I can understand that there must be some safeguards to stop people foisting absurdly long processes onto other people' computers on the internet but what about when you want to do this for yourself offline, is there any browser that can be tweaked to allow offline calculations. Not just calculations, you see, but also print outs. In browsers, it is terrific, you can use document-write to see what is going on...

    I want to be able to set a js going and leave for an hour or two or eight and come back and see a result.

    Yes, I know I maybe should use a different programming language that does not depend on browsers but I want to know if I can keep using this javascript without browsers choking (and off line). Perhaps there is a js editor that will allow printing to it without the normal browser limitations?

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    Firefox allows you to set after how long should the Stop Script dialogue box appear. I'm sure that Opera and a few others have an equivalent.

    You could also try optimising your code a bit, but by the sounds of it, that will be a pretty useless solution.
    Great wit and madness are near allied, and fine a line their bounds divide.

  3. #3
    Join Date
    Jun 2010
    Posts
    37
    Thanks for this. Yes, I am giving it a go now. I am thinking to give it much more than the 20, more like 4000!

    Does anyone know of any facility *besides browsers* (that have so many overheads and are for internet use), that can run a js program and "print" out on-screen results?

  4. #4
    Join Date
    Aug 2007
    Posts
    3,767
    SpiderMonkey is behind Firefox.

    Technically, JavaScript can only be used to refer to the scripting language used by Netscape and Mozilla, because JavaScript is just one implementation of ECMAScript. A Google search for that will turn up a few other implementations. It's main use however, is as a scripting language in browsers, as well as a few other applications (e.g. Adobe PDF's programmes as far as I know).
    Last edited by Declan1991; 08-12-2010 at 07:11 AM.
    Great wit and madness are near allied, and fine a line their bounds divide.

  5. #5
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    I want to be able to set a js going and leave for an hour or two or eight and come back and see a result.
    It's really not that difficult to do, just requires a re-thinking of your approach to the code. Why do you need to loop for that long of a time? What you need to do is break up whatever's happening in the loop in to manageable methods which can be updated with different data and run in a faux recursive fashion making use of setTimeout or setInterval. For an extremely simplified example:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>Some Title</title>
    <script type="text/javascript">
    var RECURSOR = {
        value : null,
        doCalc : function () {
            var i,
                O = this,
                el = document.getElementById('message');
            if (O.value !== null) {
                for (i = 1; i < 2001; i++) {
                    O.value += 1;
                }
                if (O.value < 100000) {
                    el.innerHTML = O.value;
                    window.setTimeout(function () { O.doCalc(); }, 2000);
                } else {
                    el.innerHTML = 'Finished! Final result is: '+ O.value;
                }
            }
        }
    };
    window.onload = function () {
        RECURSOR.value = 0;
        RECURSOR.doCalc();
    };
    </script>
    </head>
    <body>
    <div id="message"></div>
    </body>
    </html>
    I could set that up to do whatever I want and let it run for days if needed. Of course this is very simple thing here, but as I say you need to re-define how your code is running to allow it to do bits and pieces and track it's own progress as such so that it can pause to avoid excessive recursion and then pick back up again after a brief cool down so to speak, resuming with the new values.

  6. #6
    Join Date
    Apr 2006
    Location
    Houston
    Posts
    1,374
    Quote Originally Posted by patriciaaldoraz View Post
    When I run javascripts that make big calculations in web browsers, some of them say "the script is stuck in a loop....

    I want to be able to set a js going and leave for an hour or two or eight and come back and see a result....
    Try running it in an iframe.

  7. #7
    Join Date
    Jun 2010
    Posts
    37
    I sense your economy and elegance! You are way advanced from my simple efforts.

    I /had/ thought to break the task up more in manageable bits but you are saying something more subtle I think, to somehow break the calculation and make the browser reload and carry on using fed results from earlier. Wish I could work out how quite to do this.

    Suppose I wanted to search every possible way four integer variables that were not greater than about 200 could be and to identify if some relation exists between them. Sort of moron maths, different to the way Wilkins solved Fermat's last theorem (with intelligence). I know how to loop through the lot and test for a condition but how to break it up along your lines? Your example is so way above my head I cannot apply it. There! I have said it and without a drop of bourbon to give me courage.

  8. #8
    Join Date
    Feb 2009
    Location
    UK
    Posts
    142
    as the poster says.

    Divide and conquer.

    use setTimeout or setInterval.

    and maybe look at your code for efficiency. Assign variables outside loops with values and use inside loops to prevent repetitive calculation of data that doesn't change. Use of "functions" with (eg)
    Code:
    function fred(a,b){
    //calcs with a & b assigned to myResult here
     return myResult;
    }
    
    // &
    
    var bill = fred(jim,dave);
    for(etc etc).....
    in them if necessary to box-up some of the code and simplify the main body of the code.

    I did find help sites dealing with just such a subject. Go looking.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles