admin管理员组文章数量:1277299
function myFunction() {
wait(); //what I put there?
return;
}
myFunction();
//this is an event; when its triggered I want function to resume
onSomething = function() {
myFunction.resume(); //what I put there?
}
Its just a local experience. Note that while(!resume) won't work because that would prevent the event onSomething to happen.
function myFunction() {
wait(); //what I put there?
return;
}
myFunction();
//this is an event; when its triggered I want function to resume
onSomething = function() {
myFunction.resume(); //what I put there?
}
Its just a local experience. Note that while(!resume) won't work because that would prevent the event onSomething to happen.
Share Improve this question asked Nov 6, 2011 at 13:45 MaiaVictorMaiaVictor 53k47 gold badges157 silver badges301 bronze badges 1- You can do that with generators, but that's not cross-browser. It will e with ECMAScript 6 though... – Šime Vidas Commented Nov 6, 2011 at 13:51
3 Answers
Reset to default 5This is not possible.
Instead of waiting for the event, you want to put whatever you need to do when it happens into the onSomething
function.
You have to switch your brains to thinking in events instead of the "traditional" programming flow. The way to do this is:
function myFunctionPart1() {
doSomething();
}
function myFunctionPart2() {
doSomethingElse();
}
myFunctionPart1();
onSomething = function() {
myFunctionPart2();
}
So, with a generator it would look like so:
function test() {
// first part of function
yield;
// second part of function
yield;
}
var gen = test(); // creating a generator
gen.next(); // execute first part
button.onclick = function () {
gen.next(); // execute second part on button click
};
Live demo: http://jsfiddle/MQ9PT/
This however doesn't work beyond Firefox. It will bee part of the ECMAScript standard in the next edition...
本文标签: javascriptMaking a function to wait an event before returningStack Overflow
版权声明:本文标题:javascript - Making a function to wait an event before returning? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741260206a2367471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论