Repeatedly read a JSON file, refreshing sketch each time

Hi everyone, the ‘reading json example’ JSON is very close to what am needing. If could ‘loop’ this to repeatedly read json and refresh sketch, should have everything needed. Are there examples of such a ‘looping’ skeleton?:thinking:

Cheers, Noah

2 Likes

Thanks, this does seem to be headed in a good direction. Does anyone have any example code within the p5.js context? Am a complete and total beginner.:grinning_face_with_smiling_eyes: For example, can picture having no idea what ‘worker’, ‘global scope’, etc mean?

Thanks! Noah

You shouldn’t concern about what a Worker is anytime soon; if ever!

On the other hand, the ‘global scope’ means every global variable we declare and the properties that have already been set by the JavaScript engine (a.K.a. builtin objects); which can be accessed by all functions everywhere.

Those global properties can be accessed via the globalThis object btW:

Interesting! Can I ask why you feel you need to reload the JSON in a loop? Even if the data changes, there might be better approaches than repeatedly fetching from a file. What kind of data are you working with?

That said, if you really need to, you can run something every N frames using frameCount and the modulo % operator in the draw loop:

if (frameCount % 60 === 0) { // every 60 frames (~once per second at 60fps)
  console.log("refreshing...");
  // loadJSON() or whatever you want to run
}

Thanks @sableraph, this looks nice and clean! And likewise @GoToLoop, reading deeper into your links have started to get some ideas. Situation is a sixties PDP-10 simh emulation, so no ‘modern data’ connections. Currently happens to be easy to have some python robots connected to the PDP-10 and one of those can async update a json file. Here’s a nutshell summary https://gitlab.com/decwar/galaxy#project-galaxy
Cheers, Noah