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 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    rerunning code (not using loops)

    ok so how do i go about rerunning a script for a set point ? (not using loops)
    so say i was to call a function and in that function there was some sort of code that went back to say line 42 and started running the script like normal from that point no matter where it was when i called that function

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    I think we need an example.

    I'm not even sure what you want is possible...or sensible...but at least give us an example.

    Doesn't have to be an example in code. Probably better if it is not. Just describe a real scenario where you would use this.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Let me try an example.

    You have 100 lines of code that executes from top to bottom.

    If you could start at line 42, you could do all the stuff needed once the page had started up. You don't need to run lines 1 to 41 again.

    And now you have a separate set of code that, indeed, wants to start at line 42.

    The answer is easy: BREAK UP your 100 lines into two parts.
    Code:
    function part1( )
    {
        ... the old lines 1 to 41 ...
    }
    function part2( )
    {
       ... the old lines 42 to 100 ...
    }
    function main( )
    {
        part1( );
        part2( );
        // and you have effectively run from the old line 1 to the old line 100
    }
    
    function doSomethingElse( )
    {
        ... whatever ...
        ... more stuff ...
        part2( ); // run the old lines 42 to 100!
    }
    Is it as simple as that? Or do I completely misunderstand you?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    another possibility would be wrapping your code in if blocks which would keep it all in one function and let you decide which code runs according to the conditions you set:

    Code:
    function main (run1,run2){
    if (run1){
    lines 1 to 41 ...
    }
    if (run2){
    lines 42 to 100 ...
    }
    
    } 
    
    main (true, true) // runs all code
    main (true, false) // runs lines 1 to 41
    main (false, true) // runs lines 42 to 100
    main (false,false) // runs whatever else is in the function except for lines 1 to 100
    but Old Pedant is right - an actual example or better description of what you are trying to do will help find the best way to proceed.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,333
    Thanks
    11
    Thanked 587 Times in 568 Posts
    Code:
    line 1
    line 2
    line 3
    line 4
    line 5

    in JS runs the same as:

    Code:
    line 1
    line 2
    middle = function(){
     line 3
     line 4
    };
    middle();
    line 5

    and then you can call middle() from anywhere to execute that chunk of code....
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #6
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok thanks i see this will be no ezy task so i will just edit my code around this for now and re visit this later
    but feel free to continuing brain storming

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Exclamation

    Quote Originally Posted by red566 View Post
    ok thanks i see this will be no ezy task so i will just edit my code around this for now and re visit this later
    but feel free to continuing brain storming
    I might be a very easy task if you could describe what it is that you are trying to do like was requested in post #2.
    I thought post #3 was very clear. What is not so 'ezy' about it?

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    There have been several extremely simple ways to achieve what was requested that have already been posted. This is after all a really trivial thing to do.

    If none of those will do what you want then you have not explained just what it is you are trying to achieve and attempting to provide further alternatives without a better explanation of what is required is pointless.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Yeah, what they both said. <grin/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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