Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #29909
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Wait on keyboard |
| Date | 2016-03-11 17:00 +0100 |
| Organization | albasani.net |
| Message-ID | <nbuq32$gg0$1@news.albasani.net> (permalink) |
| References | <6ef267d6-7655-4699-9363-d6e0f613f899@googlegroups.com> |
I'm going to reply to the first message in this thread, because I don't
know when you'll be done replying to yourself, and I don't want to
interrupt your conversation.
I'll just pretend it was all in one message.
On 03/11/2016 11:30, jonas.thornvall@gmail.com wrote:
> I need to debug, and i real do not like the gist of firefox nor
> chrome debug.
This right here is the main problem. Everything you ask for is already
available, but you can't be bothered to learn how the debugger works. By
far the simplest solution to your problem would be getting to know your
tools.
HOWEVER. In a previous thread, I have promised to give you one answer,
ignoring your idiosyncrasies, so I'm going to give it a try.
I'm going to focus on your desire to have sleep() and wait-for-event
functions that can pause and resume a program in the middle of a loop.
The reason why this doesn't exist natively in JavaScript is mostly
historical. JavaScript was created for use in browsers, and at the time
browsers were single-process, single-thread programs. Scripts or
functions were expected to run from start to finish without
interruption. While a script was running, everything else was paused
(often including the browser's user interface, leading to nasty
freezes), so a straight sleep() would be undesired.
UI programming in JavaScript is primarily event-based: delays are
implemented by setTimeout() and event listeners, both of which work by
executing a callback function. This works, and people have been using it
for a very long time. It does, however, require a different program
structure than you may be used to from Basic or Pascal.
That said, we have a different situation today, and I don't see a
compelling reason why sleep() or wait-for-keyboard should shouldn't be
possible. More on that below.
> To be honest i find it weird that you have to add a listener and on
> key handlers it is pretty much 20 linke of code that have to be
> written.
...
> Here is the sample code someone suggested on net, and it makes you wonder.
> Do they require the input to come from a specific field in a form????
It doesn't need 20 lines, and you don't need a form field to listen for
events. People usually wrap these things into reusable functions (or use
a library) to make it less verbose, but even in vanilla JS it's simple
enough:
document.addEventListener("keydown", function (evt) {
if (evt.keyCode == 13) {
console.log("Enter key was pressed.");
}
});
That's all it takes in the minimal version.
But that's not what you're looking for, I know.
> Pseudcode.
>
> wait until key pressed.
>
> If you do not like wait(3000);
> You could do something in the line already in language.
> setKeyInput(myFunction, keyPress="32")
>
> But i prefer wait until(keyPress=="32") anyday of the year.
> And keyPress is a listener that can be turned on and off with boolean true/false.
>
> So during execution you can chose function to be called from keyboard.
> if (keyPress=="32") exit();
I'm sorry, but it's not possible to implement your preference in
JavaScript. You can't just invent new syntax.
Here's something that's actually possible. Say you have a function with
a loop, and you want to pause the loop until the space key is pressed:
function jonas (start, max)
{
for (var i = start; i <= max; i++) {
console.log("i is now", i);
wait until(keypress="32"); // syntax error
}
console.log("end");
}
With a little generator magic, you can make individual functions pause-able:
var jonas = S(function* (start, max)
{
for (var i = start; i <= max; i++) {
console.log("i is now", i);
yield jonas.waitKbd(32);
}
console.log("end");
});
Now run jonas(3,5) and watch the output in the console.
Similarly, you can use other delays:
yield jonas.sleep(1000); // wait for 1000 ms
yield jonas.waitMouse(); // wait for left click
yield jonas.waitMouse(2); // wait for right click
If that's an acceptable workaround for you, all you need is the S
function below and a current browser. Remember to
1) use `var myFuncName = ...` instead of `function myFuncName`
2) wrap your original function in a call to S()
3) use function* instead of function
The following code provides these features. Paste it somewhere above or
below your own code.
function S (genFun)
{
"use strict";
var gen;
function sleepyFun ()
{
if (!gen) {
// gen = genFun(...arguments); // meh. FF bug #1067049
gen = genFun.apply(null, arguments);
}
step();
}
function step ()
{
if (gen.next().done) {
gen = null;
}
}
function listen (evtName, cond)
{
function listener (evt)
{
if (evt && cond(evt)) {
document.removeEventListener(evtName, listener);
step();
}
}
document.addEventListener(evtName, listener);
}
sleepyFun.sleep = function (ms) {
setTimeout(() => step(), ms);
};
sleepyFun.waitKbd = function (key) {
listen("keydown", (evt) => evt.keyCode == key);
};
sleepyFun.waitMouse = function (btn = 0) {
listen("click", (evt) => evt.button === btn);
};
return sleepyFun;
}
> how can a programming language be so convoluted
> the listeners ***seem?*** to suffer from proverbial diarrhea
> It piss me off to be Frank how one can convolute straight forward
> things into some mystic listener religion?
> Isn't it evolution a bit fucking weird when something that required
> one line of code suddenly need a library?
> Oh is see i need to learn an API so i can see what fucking key i pressed
> sad glimpse of a future
> sign of retardness
> nobody think about programmers
> What the fuck went wrong?
> anal beadhead
Yes, Frank, it's a horrible horrible language. They actually expect us
to learn APIs? Who do they think they are! That's probably the reason
why JavaScript is such a niche language today.
Having posted the promised reply, I will now return to my default state
of not answering questions unless the poster invests a minimum of effort
in readability and netiquette.
- stefan
PS (group): I hacked this function together rather quickly. It works,
but I'm sure it can be improved quite a bit. If you find anything, I'd
love to hear about it.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 02:30 -0800
Re: Wait on keyboard Tim Streater <timstreater@greenbee.net> - 2016-03-11 10:37 +0000
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 02:50 -0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 03:13 -0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 03:23 -0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 03:32 -0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 03:39 -0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 04:41 -0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 05:00 -0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 05:43 -0800
Re: Wait on keyboard Tim Streater <timstreater@greenbee.net> - 2016-03-11 12:13 +0000
Re: Wait on keyboard "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-11 21:52 +0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 07:29 -0800
Re: Wait on keyboard Stefan Weiss <krewecherl@gmail.com> - 2016-03-11 17:00 +0100
Re: Wait on keyboard Stefan Weiss <krewecherl@gmail.com> - 2016-03-11 17:11 +0100
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 08:32 -0800
Re: Wait on keyboard Stefan Weiss <krewecherl@gmail.com> - 2016-03-11 20:20 +0100
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 08:55 -0800
Re: Wait on keyboard Aleksandro <aleksandro@gmx.com> - 2016-03-11 16:30 -0300
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 11:46 -0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 12:22 -0800
Re: Wait on keyboard Aleksandro <aleksandro@gmx.com> - 2016-03-11 17:25 -0300
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 12:32 -0800
Re: Wait on keyboard Aleksandro <aleksandro@gmx.com> - 2016-03-11 17:42 -0300
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 12:52 -0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-11 12:47 -0800
Re: Wait on keyboard "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-03-11 18:39 -0800
Re: Wait on keyboard "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-03-11 18:37 -0800
Re: Wait on keyboard John Harris <niam@jghnorth.org.uk.invalid> - 2016-03-12 13:16 +0000
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-12 05:57 -0800
Re: Wait on keyboard John Harris <niam@jghnorth.org.uk.invalid> - 2016-03-13 10:31 +0000
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-13 05:55 -0700
Re: Wait on keyboard "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-13 17:19 +0100
Re: Wait on keyboard Scott Sauyet <scott.sauyet@gmail.com> - 2016-03-13 13:01 -0700
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-13 15:19 -0700
Re: Wait on keyboard Aleksandro <aleksandro@gmx.com> - 2016-03-13 20:08 -0300
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-13 21:45 -0700
Re: Wait on keyboard Aleksandro <aleksandro@gmx.com> - 2016-03-14 01:50 -0300
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-13 22:39 -0700
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-13 22:46 -0700
Re: Wait on keyboard Scott Sauyet <scott.sauyet@gmail.com> - 2016-03-14 11:54 -0700
Re: Wait on keyboard Tim Streater <timstreater@greenbee.net> - 2016-03-14 08:31 +0000
Re: Wait on keyboard John Harris <niam@jghnorth.org.uk.invalid> - 2016-03-14 10:43 +0000
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-12 06:04 -0800
Re: Wait on keyboard "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-12 15:22 +0100
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-12 07:46 -0800
Re: Wait on keyboard Tim Streater <timstreater@greenbee.net> - 2016-03-12 15:52 +0000
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-12 09:25 -0800
Re: Wait on keyboard Aleksandro <aleksandro@gmx.com> - 2016-03-12 20:16 -0300
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-12 15:57 -0800
Re: Wait on keyboard Aleksandro <aleksandro@gmx.com> - 2016-03-12 21:07 -0300
Re: Wait on keyboard Scott Sauyet <scott.sauyet@gmail.com> - 2016-03-12 21:27 -0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-13 03:08 -0700
Re: Wait on keyboard "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-12 23:30 +0100
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-12 06:15 -0800
Re: Wait on keyboard "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-03-11 18:33 -0800
Re: Wait on keyboard jonas.thornvall@gmail.com - 2016-03-12 03:01 -0800
Re: Wait on keyboard "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-03-11 18:30 -0800
Re: Wait on keyboard Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-12 04:44 +0100
Re: Wait on keyboard Stefan Weiss <krewecherl@gmail.com> - 2016-03-12 11:21 +0100
Re: Wait on keyboard Stefan Weiss <krewecherl@gmail.com> - 2016-03-12 11:27 +0100
Re: Wait on keyboard Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-12 14:44 +0100
Re: Wait on keyboard "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-14 18:58 +0800
csiph-web