a tough question

M

Mr Shore

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?
 
J

Joost Diepenmaat

Mr Shore said:
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.
 
M

Mr Shore

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?
 
J

Joost Diepenmaat

Mr Shore said:
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.
 
M

Mr Shore

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.

ok,seems i have to give up that idea
thanks for the quick reply
 
M

Mr Shore

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.

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
 
J

Joost Diepenmaat

Mr Shore said:
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>
 
M

Mr Shore

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>

thanks a lot
finally i tried your version,
which passed the function string then eval
 
J

Joost Diepenmaat

Mr Shore said:
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().
 
M

Mr Shore

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().

but I don't know how to pass an executing function directly?
 
J

Joost Diepenmaat

Mr Shore said:
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);
 
M

Mr Shore

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);
got it!
thanks a lot:)
 
M

Mr Shore

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
 
S

slebetman

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'
});
 
K

Kris Zyp

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
 
D

Dr J R Stockton

Sun said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top