Is there a simpler way to do this?

Discussion in 'Javascript' started by Ritchie Valens, Jul 30, 2011.

  1. Hi,



    I was wondering, how different is Javascript, from Actionscript 3.0?

    I am messing around with a web radio application, I am trying to
    create.

    I am a newbie but more used to AS 2.0, but I finally took the plunge
    and forced myself to learn AS 3.0.

    Basically everytime a button is pressed, it goes to the next Radio
    Station:

    As Below, but I think there must be a simpler way, without repeating
    the code.

    Any suggestions,

    Please keep it simple, and would you be kind enough to explain, what
    you have done?


    The right Braces at the end bother me, they look silly, which is why I
    thought there is an easier way






    Regards





    //Stop Script

    stop();


    // up Button Script Jazz fm

    up_btn.addEventListener(MouseEvent.CLICK, btn1);
    function btn1 ( event:MouseEvent):void
    { navigateToURL (new URLRequest ("http://www.jazzfm.com"));

    gotoAndStop(2);


    up_btn.addEventListener(MouseEvent.CLICK, btn1);
    function btn1 ( event:MouseEvent):void
    { navigateToURL (new URLRequest ("http://www.bbc.co.uk/radio1"));


    up_btn.addEventListener(MouseEvent.CLICK, btn1);
    function btn1 ( event:MouseEvent):void
    { navigateToURL (new URLRequest ("http://www.bbc.co.uk/radio2"));


    up_btn.addEventListener(MouseEvent.CLICK, btn1);
    function btn1 ( event:MouseEvent):void
    { navigateToURL (new URLRequest ("http://www.bbc.co.uk/radio3"));


    up_btn.addEventListener(MouseEvent.CLICK, btn1);
    function btn1 ( event:MouseEvent):void
    { navigateToURL (new URLRequest ("http://www.bbc.co.uk/radio4"));


    }}}}}
    Life is Good !!!
     
    Ritchie Valens, Jul 30, 2011
    #1
    1. Advertisements

  2. Any Suggestions?



    Life is Good !!!
     
    Ritchie Valens, Jul 30, 2011
    #2
    1. Advertisements

  3. Ritchie Valens

    Luuk Guest

    Luuk, Jul 30, 2011
    #3
  4. Ritchie Valens, Jul 31, 2011
    #4
  5. This news group is on topic for *any* language derived from ECMAScript.
    That includes ActionScript as well as JScript and JavaScript.

    That said, ActionScript has a lot of features that are not in the
    current JavaScript and JScript languages so answers might be slow in
    arriving.

    John
     
    John G Harris, Jul 31, 2011
    #5
  6. John,

    Thanks.

    Regards


    Life is Good !!!
     
    Ritchie Valens, Jul 31, 2011
    #6
  7. Ritchie Valens

    Scott Sauyet Guest

    You're instincts are right. There is a cleaner way. There must be.
    I have one below in regular javascript. I can't imagine it would be
    difficult to do in ActionScript.
    Here's one approach:

    <p>Now playing: <span id="result">Current Station will
    go here</span>.</p>
    <button id="next" onclick="nextStation()">Next</button>

    var urls = [
    "http://www.jazzfm.com",
    "http://www.bbc.co.uk/radio1",
    "http://www.bbc.co.uk/radio2",
    "http://www.bbc.co.uk/radio3",
    "http://www.bbc.co.uk/radio4"
    ];
    nextStation = (function() {
    var result = document.getElementById("result"),
    count = urls.length,
    current = count;
    return function() {
    current = (current + 1) % count
    result.innerHTML = urls[current]; // change as needed
    };
    }());

    You can see this approach at

    <http://jsfiddle.net/CrossEye/a4KLJ/>

    (Sorry this is so late. Just returned from a vacation.)

    -- Scott
     
    Scott Sauyet, Aug 16, 2011
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.