By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,460 Members | 1,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,460 IT Pros & Developers. It's quick & easy.

a tough question

P: n/a
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?
Jun 27 '08 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Mr Shore <sh*********@gmail.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/
Jun 27 '08 #2

P: n/a
On Apr 14, 2:07 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Mr Shore <shore.cl...@gmail.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?
Jun 27 '08 #3

P: n/a
Mr Shore <sh*********@gmail.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(value) {
// 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/
Jun 27 '08 #4

P: n/a
On Apr 14, 2:37 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Mr Shore <shore.cl...@gmail.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(value) {
// 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
Jun 27 '08 #5

P: n/a
On Apr 14, 2:37 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Mr Shore <shore.cl...@gmail.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(value) {
// 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
Jun 27 '08 #6

P: n/a
Mr Shore <sh*********@gmail.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:arguments>

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #7

P: n/a
VK
On Apr 13, 11:10 pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
Anyone actually setting undefined to some defined value
should of course have their computer taken away.
Thank you, bro! :-) :-|
Jun 27 '08 #8

P: n/a
On Apr 14, 3:10 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Mr Shore <shore.cl...@gmail.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:arguments>

--
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
Jun 27 '08 #9

P: n/a
Mr Shore <sh*********@gmail.comwrites:
On Apr 14, 3:10 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
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/
Jun 27 '08 #10

P: n/a
On Apr 14, 5:25 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Mr Shore <shore.cl...@gmail.comwrites:
On Apr 14, 3:10 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
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?
Jun 27 '08 #11

P: n/a
Mr Shore <sh*********@gmail.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(fun) {
var value = 42;
fun(value);
}

call_me_back(function(value) {
alert(value);
});

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

call_me_back(cb);
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #12

P: n/a
On Apr 14, 5:32 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Mr Shore <shore.cl...@gmail.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(fun) {
var value = 42;
fun(value);

}

call_me_back(function(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:)
Jun 27 '08 #13

P: n/a
On Apr 14, 5:42 am, Mr Shore <shore.cl...@gmail.comwrote:
On Apr 14, 5:32 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Mr Shore <shore.cl...@gmail.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(fun) {
var value = 42;
fun(value);
}
call_me_back(function(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
Jun 27 '08 #14

P: n/a
On Apr 14, 5:51 am, Mr Shore <shore.cl...@gmail.comwrote:
On Apr 14, 5:42 am, Mr Shore <shore.cl...@gmail.comwrote:
On Apr 14, 5:32 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Mr Shore <shore.cl...@gmail.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(fun) {
var value = 42;
fun(value);
}
call_me_back(function(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'
});
Jun 27 '08 #15

P: n/a
On Apr 13, 12:05 pm, Mr Shore <shore.cl...@gmail.comwrote:
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?
This capability is called coroutines, and is a part of some functional
languages, and is a powerful level of expressibility, particularly in
UI applications. It is not natively supported in JavaScript (although
hidden under the covers of FF3's new synchronous XHR handler).
JavaScript Strands is a framework that provides this capability
through code compilation (which is definitely not everyone's cup of
tea):
http://www.xucia.com/page/Strands
Kris
Jun 27 '08 #16

P: n/a
In comp.lang.javascript message <87************@zeekat.nl>, Sun, 13 Apr
2008 21:10:40, Joost Diepenmaat <jo***@zeekat.nlposted:
>
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.
However, var U apparently generates a known U whose value is
genuinely undefined even if the variable undefined has been defined.
One can use ==U or ===U, and get true if the LHS is not defined.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jun 27 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.