# Repeatedly read a JSON file, refreshing sketch each time

**URL:** <https://discourse.processing.org/t/repeatedly-read-a-json-file-refreshing-sketch-each-time/47053>\
**Category:** Coding Questions\
**Created:** [August 30, 2025, 5:06am UTC](https://discourse.processing.org/t/repeatedly-read-a-json-file-refreshing-sketch-each-time/47053 "2025-08-30T05:06:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![noahhsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noahhsmith/32/21058_2.png) [@noahhsmith](https://discourse.processing.org/u/noahhsmith)\
**Post date:** [August 30, 2025, 5:06am UTC](https://discourse.processing.org/t/repeatedly-read-a-json-file-refreshing-sketch-each-time/47053/1 "2025-08-30T05:06:05Z")

</div>

Hi everyone, the ‘reading json example’ [JSON](https://p5js.org/examples/loading-and-saving-data-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?🤔

Cheers, Noah

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 30, 2025, 7:25am UTC](https://discourse.processing.org/t/repeatedly-read-a-json-file-refreshing-sketch-each-time/47053/2 "2025-08-30T07:25:02Z")

</div>

> **[WorkerGlobalScope: setInterval() method - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/setInterval)**
>
> The setInterval() method of the WorkerGlobalScope interface repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.

---

<div class="post-metadata">

**Author:** ![noahhsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noahhsmith/32/21058_2.png) [@noahhsmith](https://discourse.processing.org/u/noahhsmith)\
**Post date:** [August 31, 2025, 6:04am UTC](https://discourse.processing.org/t/repeatedly-read-a-json-file-refreshing-sketch-each-time/47053/3 "2025-08-31T06:04:28Z")

</div>

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.😄 For example, can picture having no idea what ‘worker’, ‘global scope’, etc mean?

Thanks! Noah

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 31, 2025, 6:20am UTC](https://discourse.processing.org/t/repeatedly-read-a-json-file-refreshing-sketch-each-time/47053/4 "2025-08-31T06:20:44Z")

</div>

> [@noahhsmith](#):
>
> For example, can picture having no idea what ‘worker’, ‘global scope’, etc mean?

You shouldn’t concern about what a [Worker](https://developer.mozilla.org/en-US/docs/Web/API/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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects)); which can be accessed by all functions everywhere.

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

> **[globalThis - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis)**
>
> The globalThis global property contains the global this value, which is usually akin to the global object.

---

<div class="post-metadata">

**Author:** ![sableraph](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sableraph/32/251_2.png) [@sableraph](https://discourse.processing.org/u/sableraph)\
**Post date:** [August 31, 2025, 7:45am UTC](https://discourse.processing.org/t/repeatedly-read-a-json-file-refreshing-sketch-each-time/47053/5 "2025-08-31T07:45:33Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![noahhsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noahhsmith/32/21058_2.png) [@noahhsmith](https://discourse.processing.org/u/noahhsmith)\
**Post date:** [August 31, 2025, 12:35pm UTC](https://discourse.processing.org/t/repeatedly-read-a-json-file-refreshing-sketch-each-time/47053/6 "2025-08-31T12:35:06Z")

</div>

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](https://gitlab.com/decwar/galaxy#project-galaxy)  
Cheers, Noah
