#How do I stop JS from fetching two functions at the same time?

1 messages · Page 1 of 1 (latest)

boreal iris
#

This should be fairly simple but I am a complete baby in JS.
I am using this joystick template: https://github.com/jeromeetienne/virtualjoystick.js
and I based the site off the basic.html example.

When the joystick goes 'up left' it calls both /left and /forward, same for right .
Who do I make it so joystick.up() overrides left and right when combined?

GitHub

a virtual joystick library to emulate a joystick on touch screen in javascript - GitHub - jeromeetienne/virtualjoystick.js: a virtual joystick library to emulate a joystick on touch screen in javas...

upper plaza
#

forgive me for I am not that familiar with JS but it looks like you would just remove the addition of ‘left’ and ‘forward’ from that ‘setInterval’ function

#

That or you could write some JS that reads the values in, determines which movement is greater ( up vs left) etc and then decide what to update based on the greater value

boreal iris
#

thnx for ur input but it seems to be doing the same

boreal iris
upper plaza
#

Kinda looks like you should be able to do this off of dx/dy too

#

Don’t use right left up down and just look at dx dy like the basic demo in examples

boreal iris
#

mhm

upper plaza
#

I’m that example they don’t tie a fetch into up/down/left/right then you could sum the dx dy like the example and figure out if it’s just moving left or right

boreal iris
#

mhm

#

i did take ur advice a little and seperate the fetch from the direction display

#

but i might try what u said rn

upper plaza
#

hope I helped - it’s like 6am here and I just got up. I’m sure someone else will chime in soon if you don’t get it

boreal iris
#

yea man thanks a ton i rlly appreciate it

boreal iris
#

I solved it

#

not by the addition but by combining them the right way

storm ermine
#

also fetch() is async and your setInterval() can double things up

#

also why not extract like 4 local boolean variables... to make the code simpler

jagged wolf
#

@boreal iris fetch is used to load data across a network and will be very slow, are you sure you do not just want to call a function?

#

I think you may be confused as to what the fetch api is

storm ermine
#

you should probably cache those 4 HTML pages inside a JSON object on page load

#

and then have your code innerHTML the CACHED version

#

also you are doing this every 300ms like seriously 😮

#

otherwise, you should have the fetch callback redo a debounce setTimeout()
instead of having setInterval() because otherwise, if the fetch() is slow you will have some sort of timeout infinite loop in the event loop