a tough question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mr Shore

    a tough question

    hi all
    what I want to do is as below
    start a function
    halt on,
    until some event occurs(maybe user click)
    and then return some value accordingly.
    Or is it possible to do this stuff?
  • Joost Diepenmaat

    #2
    Re: a tough question

    Mr Shore <shore.cloud@gm ail.comwrites:
    hi all
    what I want to do is as below
    start a function
    halt on,
    until some event occurs(maybe user click)
    and then return some value accordingly.
    Or is it possible to do this stuff?
    No, and you don't want to do that anyway. Just do whatever it is you
    need to do from the onclick handler. Don't fight the event model.


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

    Comment

    • Mr Shore

      #3
      Re: a tough question

      On Apr 14, 2:07 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
      Mr Shore <shore.cl...@gm ail.comwrites:
      hi all
      what I want to do is as below
      start a function
      halt on,
      until some event occurs(maybe user click)
      and then return some value accordingly.
      Or is it possible to do this stuff?
      >
      No, and you don't want to do that anyway. Just do whatever it is you
      need to do from the onclick handler. Don't fight the event model.
      >
      --
      Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
      so what do you think of the prompt function?

      Comment

      • Joost Diepenmaat

        #4
        Re: a tough question

        Mr Shore <shore.cloud@gm ail.comwrites:
        >No, and you don't want to do that anyway. Just do whatever it is you
        >need to do from the onclick handler. Don't fight the event model.
        >
        so what do you think of the prompt function?
        It's a hack; it prevents the user from doing anything else while the
        prompt is active, and there is no way to replicate the behaviour with
        user-written javascript. The "best" you can do is to disable everything
        else on the page (using some kind of overlay, for instance). That
        /still/ won't give you the "call function and use return value"
        mechanism, and there is no reason you need that - the event model is
        much more flexible.

        You just need

        show_my_prompt( data,function(v alue) {
        // do stuff with value
        });

        instead of

        var value = prompt(data);
        // do stuff with value.

        --
        Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

        Comment

        • Mr Shore

          #5
          Re: a tough question

          On Apr 14, 2:37 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
          Mr Shore <shore.cl...@gm ail.comwrites:
          No, and you don't want to do that anyway. Just do whatever it is you
          need to do from the onclick handler. Don't fight the event model.
          >
          so what do you think of the prompt function?
          >
          It's a hack; it prevents the user from doing anything else while the
          prompt is active, and there is no way to replicate the behaviour with
          user-written javascript. The "best" you can do is to disable everything
          else on the page (using some kind of overlay, for instance). That
          /still/ won't give you the "call function and use return value"
          mechanism, and there is no reason you need that - the event model is
          much more flexible.
          >
          You just need
          >
          show_my_prompt( data,function(v alue) {
          // do stuff with value
          >
          });
          >
          instead of
          >
          var value = prompt(data);
          // do stuff with value.
          >
          --
          Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
          ok,seems i have to give up that idea
          thanks for the quick reply

          Comment

          • Mr Shore

            #6
            Re: a tough question

            On Apr 14, 2:37 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
            Mr Shore <shore.cl...@gm ail.comwrites:
            No, and you don't want to do that anyway. Just do whatever it is you
            need to do from the onclick handler. Don't fight the event model.
            >
            so what do you think of the prompt function?
            >
            It's a hack; it prevents the user from doing anything else while the
            prompt is active, and there is no way to replicate the behaviour with
            user-written javascript. The "best" you can do is to disable everything
            else on the page (using some kind of overlay, for instance). That
            /still/ won't give you the "call function and use return value"
            mechanism, and there is no reason you need that - the event model is
            much more flexible.
            >
            You just need
            >
            show_my_prompt( data,function(v alue) {
            // do stuff with value
            >
            });
            >
            instead of
            >
            var value = prompt(data);
            // do stuff with value.
            >
            --
            Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
            still a question
            how to pass variable parameters in js?
            function t(a,b){
            alert(b);
            }

            t(b="hi");
            which i meant to set second parameter of function t as "hi" but failed

            Comment

            • Joost Diepenmaat

              #7
              Re: a tough question

              Mr Shore <shore.cloud@gm ail.comwrites:
              still a question
              how to pass variable parameters in js?
              function t(a,b){
              alert(b);
              }
              >
              t(b="hi");
              which i meant to set second parameter of function t as "hi" but failed
              Javascript only does positional parameters. Just do

              t(undefined,"hi ");

              or whatever you want to indicate "this parameter is useless" for the
              first parameter. A possible downside to using undefined is that
              undefined is a variable, and so can be set to a value that isn't
              undefined. Anyone actually setting undefined to some defined value
              should of course have their computer taken away.

              See also: <http://developer.mozilla.org/en/docs/
              Core_JavaScript _1.5_Reference: Functions:argum ents>

              --
              Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

              Comment

              • VK

                #8
                Re: a tough question

                On Apr 13, 11:10 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
                Anyone actually setting undefined to some defined value
                should of course have their computer taken away.
                Thank you, bro! :-) :-|

                Comment

                • Mr Shore

                  #9
                  Re: a tough question

                  On Apr 14, 3:10 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
                  Mr Shore <shore.cl...@gm ail.comwrites:
                  still a question
                  how to pass variable parameters in js?
                  function t(a,b){
                  alert(b);
                  }
                  >
                  t(b="hi");
                  which i meant to set second parameter of function t as "hi" but failed
                  >
                  Javascript only does positional parameters. Just do
                  >
                  t(undefined,"hi ");
                  >
                  or whatever you want to indicate "this parameter is useless" for the
                  first parameter. A possible downside to using undefined is that
                  undefined is a variable, and so can be set to a value that isn't
                  undefined. Anyone actually setting undefined to some defined value
                  should of course have their computer taken away.
                  >
                  See also: <http://developer.mozilla.org/en/docs/
                  Core_JavaScript _1.5_Reference: Functions:argum ents>
                  >
                  --
                  Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
                  thanks a lot
                  finally i tried your version,
                  which passed the function string then eval

                  Comment

                  • Joost Diepenmaat

                    #10
                    Re: a tough question

                    Mr Shore <shore.cloud@gm ail.comwrites:
                    On Apr 14, 3:10 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
                    thanks a lot
                    finally i tried your version,
                    which passed the function string then eval
                    That's not what my version was intended to do. Javascript has proper
                    function objects, meaning you can pass functions around and call them
                    without the error prone and slow use of eval().

                    --
                    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

                    Comment

                    • Mr Shore

                      #11
                      Re: a tough question

                      On Apr 14, 5:25 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
                      Mr Shore <shore.cl...@gm ail.comwrites:
                      On Apr 14, 3:10 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
                      thanks a lot
                      finally i tried your version,
                      which passed the function string then eval
                      >
                      That's not what my version was intended to do. Javascript has proper
                      function objects, meaning you can pass functions around and call them
                      without the error prone and slow use of eval().
                      >
                      --
                      Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
                      but I don't know how to pass an executing function directly?

                      Comment

                      • Joost Diepenmaat

                        #12
                        Re: a tough question

                        Mr Shore <shore.cloud@gm ail.comwrites:
                        but I don't know how to pass an executing function directly?
                        I really hope that one day there will be a seriously good book about
                        javascript. Anyway:

                        function call_me_back(fu n) {
                        var value = 42;
                        fun(value);
                        }

                        call_me_back(fu nction(value) {
                        alert(value);
                        });

                        function cb(v) {
                        alert("V: "+v);
                        }

                        call_me_back(cb );


                        --
                        Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

                        Comment

                        • Mr Shore

                          #13
                          Re: a tough question

                          On Apr 14, 5:32 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
                          Mr Shore <shore.cl...@gm ail.comwrites:
                          but I don't know how to pass an executing function directly?
                          >
                          I really hope that one day there will be a seriously good book about
                          javascript. Anyway:
                          >
                          function call_me_back(fu n) {
                          var value = 42;
                          fun(value);
                          >
                          }
                          >
                          call_me_back(fu nction(value) {
                          alert(value);
                          >
                          });
                          >
                          function cb(v) {
                          alert("V: "+v);
                          >
                          }
                          >
                          call_me_back(cb );
                          >
                          --
                          Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
                          got it!
                          thanks a lot:)

                          Comment

                          • Mr Shore

                            #14
                            Re: a tough question

                            On Apr 14, 5:42 am, Mr Shore <shore.cl...@gm ail.comwrote:
                            On Apr 14, 5:32 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
                            >
                            Mr Shore <shore.cl...@gm ail.comwrites:
                            but I don't know how to pass an executing function directly?
                            >
                            I really hope that one day there will be a seriously good book about
                            javascript. Anyway:
                            >
                            function call_me_back(fu n) {
                            var value = 42;
                            fun(value);
                            >
                            }
                            >
                            call_me_back(fu nction(value) {
                            alert(value);
                            >
                            });
                            >
                            function cb(v) {
                            alert("V: "+v);
                            >
                            }
                            >
                            call_me_back(cb );
                            >
                            --
                            Joost Diepenmaat | blog:http://joost.zeekat.nl/|work:http://zeekat.nl/
                            >
                            got it!
                            thanks a lot:)
                            but now i think maybe i'll stick to the error prone though eval
                            version
                            because otherwise it'll be quite hard for me to make it work in all
                            occasions,eg. the variable num of parameters and so on
                            error prone or general
                            i choose the later

                            Comment

                            • slebetman

                              #15
                              Re: a tough question

                              On Apr 14, 5:51 am, Mr Shore <shore.cl...@gm ail.comwrote:
                              On Apr 14, 5:42 am, Mr Shore <shore.cl...@gm ail.comwrote:
                              >
                              >
                              >
                              On Apr 14, 5:32 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
                              >
                              Mr Shore <shore.cl...@gm ail.comwrites:
                              but I don't know how to pass an executing function directly?
                              >
                              I really hope that one day there will be a seriously good book about
                              javascript. Anyway:
                              >
                              function call_me_back(fu n) {
                              var value = 42;
                              fun(value);
                              >
                              }
                              >
                              call_me_back(fu nction(value) {
                              alert(value);
                              >
                              });
                              >
                              function cb(v) {
                              alert("V: "+v);
                              >
                              }
                              >
                              call_me_back(cb );
                              >
                              --
                              Joost Diepenmaat | blog:http://joost.zeekat.nl/|work:http://zeekat.nl/
                              >
                              got it!
                              thanks a lot:)
                              >
                              but now i think maybe i'll stick to the error prone though eval
                              version
                              because otherwise it'll be quite hard for me to make it work in all
                              occasions,eg. the variable num of parameters and so on
                              error prone or general
                              i choose the later
                              For variable numbers of parameters and named parameters the most
                              "natural" way I've found is to write functions that accept ONE
                              variable which is an object:

                              function doSomething(obj ) {
                              var a = obj.a;
                              var b = obj.b;

                              alert(a+','+b);
                              }

                              doSomething({ a:100 });
                              doSomething({ b:'Hello' });
                              doSomething({
                              a:'Hi',
                              b:'Bye'
                              });

                              Comment

                              Working...