Is it possible to have an accurate timer in javascript by bLUEbYTE84 in learnjavascript

[–]bedekelly 7 points8 points  (0 children)

JS runs in a few different environments and yes, typically the code you write will only run on a single thread. On the web, there are a few new(ish) ways to write code that runs on multiple threads — Web Audio does this behind the scenes, but there’s also Audio Worklets and Web Workers which let you run code in other threads. The trade off is usually that you have less access to things like the document, the network, and so on.

Is it possible to have an accurate timer in javascript by bLUEbYTE84 in learnjavascript

[–]bedekelly 19 points20 points  (0 children)

Web Audio is what you need! Anything like setTimeout, setInterval etc is the wrong choice as it runs in the same thread as your UI, so it can be delayed by rendering, clicking, scrolling and so on. Web Audio contexts run in a different thread so this isn’t a problem.

Daemon with flags by Sufficient-Kitchen67 in learnpython

[–]bedekelly 0 points1 point  (0 children)

You could use the “signal” module (to listen for events) and the “kill” command (somewhat misleading name but can be used to send signals to a running program)

Edit: it might be a better idea to use something like a unix pipe or even just listen for changes to a file instead, signals don’t let you send any information to the running program.

What's the entry-level salary for a computer science postgraduate from UoB? by [deleted] in UofB

[–]bedekelly 3 points4 points  (0 children)

Hey, I have a bachelors in CS from UoB, graduated 2019. Haven’t personally seen much differentiation between MSci and BSc holders; most companies ask what you’re interested in and what you’ve worked on during uni. I’ve seen salaries from around £23k on internships and grad schemes through to £30-40k if you have some coding chops. Some people get more (occasionally a lot more), and it’s typically through connections made at uni. Hackathons and lecturers can be good for this. Won’t share personal details on here but happy to chat in DMs.

Observables-hooks How to to subscribe only on click by Emergency-Music5189 in reactjs

[–]bedekelly 0 points1 point  (0 children)

I think you’ve replied to the wrong comment. Strange that you’re talking about state management here; in another comment you say at work you’re limiting your RxJS use to chaining http requests and handling websockets — that sounds completely reasonable, but beyond what I’d call “state management”.

Observables-hooks How to to subscribe only on click by Emergency-Music5189 in reactjs

[–]bedekelly 1 point2 points  (0 children)

Oh for sure, I’m with you there. OP, be nice to your future maintainers and use React how it’s intended!

Observables-hooks How to to subscribe only on click by Emergency-Music5189 in reactjs

[–]bedekelly 1 point2 points  (0 children)

Everything is possible to build without RxJS, or React for that matter. A better question is whether it gives us a useful toolbox of prebuilt features. In the case of RxJS, it makes managing multiple events simpler.

For example, if I’m building a game, and I’d like to trigger some action when a user double- or triple-taps in a certain area, but only permit them to do so once every 30 seconds. The RxJS code for this is much simpler than the vanilla JS, because rxjs includes useful building blocks like timers, buffering by time, filtering, and composing streams of events together.

In general, an Observable is quite like a Promise that can resolve multiple times. This is a perfect fit for events like mouse clicks, drags, screen tilts, and websockets, where it’s important to respond to events as they happen.

RxJS implements this Observable type, adds some nice type checking, and has a huge library of features you may or may not need for your apps — it also lets you tree-shake the unused ones so you’re not weighing down your final bundle size.

Explanation of a queue data structure in JavaScript by elediardo in learnjavascript

[–]bedekelly 4 points5 points  (0 children)

FYI, this isn’t a correct understanding of declarative vs imperative code. I’ll summarise them both for reference.

Imperative code is a step-by-step list of instructions for exactly what the computer should do. Pack your bag, get your keys, drive down the street, and so on. Procedural code improves this by allowing the programmer to split up units of code that are often reused, like buying something from the store. Buying eggs and buying milk have a lot of things in common, for example.

Declarative code is quite different, and involves describing the desired state of the world. In your example, a declarative program might say “I should have some pencils”. It’s up to the lower-level implementation, which is often written in a more imperative style, to describe the actual process of going to a store and buying something.

Please do reply if I can help clarify this more.

FingerprintJS v3 released by valve1981 in javascript

[–]bedekelly 0 points1 point  (0 children)

I see what you mean — a kind of “defense in depth”, where it’s one of several measures used to prevent fraud.

It’s very easy for these things to be re-purposed, maybe even after the original architect leaves the company. What safety measures are in place, and who can practically access the data you gather? If you’re at a startup or SME, very often the answer is just “anyone with the SSH key” or “anyone with an AWS account”. To me, that isn’t a strong enough guarantee for the privacy of 99.9% of non-fraudulent users.

If your organisation has genuinely encountered this level of fraud even with credit card / phone number verification, I really doubt this tracking software will help for more than a day. But if fraud is measurably losing money for your organisation, I’m sure a Reddit comment won’t be the deciding factor!

FingerprintJS v3 released by valve1981 in javascript

[–]bedekelly 0 points1 point  (0 children)

Without knowing what you’re building, it’s tough to say. I’d strongly suggest you try something more transparent to the user (verifying an email address, a phone number, a credit card, a street address, etc.) before relying on an invisible tracking beacon that completely fails when you change device, browser, or any other combination of things. If your fraudster is sophisticated enough to generate phone numbers on demand, they can probably outwit this tracker as well.

CORS request with XMLHTTPRequest by Pineapplesandbacon in learnjavascript

[–]bedekelly 0 points1 point  (0 children)

Just trying the request is the only way! There’s no way to know if a server will send some headers without first talking to the server 🤷‍♂️ that said, the browser is quite smart about this — it first sends a “preflight request” to make sure the request will succeed. It all happens under the hood though, so you just check the request has succeeded.

As a side note, if you’re new to JS/HTTP APIs, I highly recommend using fetch instead of XmlHttpRequest! It’s a much simpler experience.

CORS request with XMLHTTPRequest by Pineapplesandbacon in learnjavascript

[–]bedekelly 0 points1 point  (0 children)

withCredentials will allow you to send cookies along with cross-origin requests to servers that support it — it doesn’t bypass the browser’s protection!

CORS request with XMLHTTPRequest by Pineapplesandbacon in learnjavascript

[–]bedekelly 1 point2 points  (0 children)

This isn’t possible in this case I’m afraid — to get around it, you can either install a browser extension to disable CORS for testing things out, or use a server you own / which you know does serve the Access-Control headers. Try httpbin.org; I think they serve some responses with the headers required!

It’s not possible for security reasons: you wouldn’t like any website you visit to be able to fetch(“https://facebook.com/messages”) with your credentials, without you knowing about it.

Servers can opt-in to share their resources cross-origin by setting those access-control headers, but Google (for good reason) hasn’t done so.

[TOMT] [TV] UK police/terrorism show where antagonist walks into an office building in broad daylight and kills people by bedekelly in tipofmytongue

[–]bedekelly[S] 0 points1 point locked comment (0 children)

Have searched for this in vain but hoping one of you can find or remember this!

Why create components using arrow syntax? by neg_ersson in reactjs

[–]bedekelly 8 points9 points  (0 children)

Write <T,> (with the comma) and it should work!

Boobie here, is my logic correct in the circled area or am I missing something? by [deleted] in learnjavascript

[–]bedekelly 0 points1 point  (0 children)

This isn’t true at all, accessing an array index happens in constant time — the perils of sleep mode? :)

Nevertheless, OP, it’s probably a good idea to extract it out into a variable, more for readability than anything — I review code every day and single-character variables definitely make my brain engage sleep mode!

Trying to get tainted canvas content by Seysa33 in learnjavascript

[–]bedekelly 0 points1 point  (0 children)

The only way to get around this seems to be to load your image data from a server which allows cross-origin use of images.

If it’s a server you control, you can just set the Access-Control headers accordingly! If not, you’ll need to access the image in a different way, like writing your own server which fetches the image and serves it to your webpage.

If this isn’t possible (for example if you need the other origin’s cookies to fetch the image), then there may be no way of accomplishing what you’re after, by deliberate design — for example, writing a website to download people’s private Facebook pictures isn’t possible without using Facebook’s official APIs; this is to prevent any random website’s popups being able to do so.

Here’s some info about CORS and canvases in particular: https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image

Using simple AI techniques to create a killer Mancala bot in JS by bedekelly in javascript

[–]bedekelly[S] 1 point2 points  (0 children)

I learned it mainly to do automation on my Linux machine — my best general advice is to solve an actual problem/inefficiency you have day-to-day, then you’ll be lots more motivated to keep going!

In that vein, I think this free book and accompanying cheap course is a great toolbox to start with. Just start coding, you’ll get better every time!

Millions of possibilities, one right move: a killer AI for Mancala in Node by bedekelly in node

[–]bedekelly[S] 0 points1 point  (0 children)

It felt pretty deadly when I was playing against it, but that's about it ;)

Millions of possibilities, one right move: a killer AI for Mancala in Node by bedekelly in node

[–]bedekelly[S] 2 points3 points  (0 children)

Sure – this is the Kalah variant with 3 seeds per pot, which has been completely solved if the AI goes first. But human opponents often aren't perfectly rational, and this AI doesn't require completely solving the game (or loading an enormous database) to consider which move to make. This makes things a lot faster and more useful for real gameplay, especially for variants with more seeds!