Changing image using setInterval() with class in p5.js

I'm currently tackling a project involving p5.js.

My objective is to rotate images every X seconds, similar to how it was achieved in this example here.

Nevertheless, I encountered an issue when attempting the same with a class object. The setInterval() method seems to malfunction in this scenario.

Could this be related to scope in some way?

Answer №1

Let's delve into a more straightforward example:

class Example {
  constructor(exampleMessage) {
    this.exampleMessage = exampleMessage;
  }
  display() {
    print(this.exampleMessage);
  }
}

What do you think will happen with the following code?

var message = 'hi';
var example = new Example(message);
message = 'bye';
example.display();

Based on how your code is set up, it may seem like 'bye' would be printed, but in reality, 'hi' will be displayed.

This discrepancy occurs because you are passing the value of the variable to the Example constructor. This creates two variables referencing the same value 'hi'. When you update the first variable, the second one doesn't automatically reflect that change.

To address this issue, you could utilize a single global variable like so:

class Example {
  display() {
    print(message);
  }
}

var message = 'experimenting';
var example = new Example();
example.display();

Alternatively, you could implement a function such as setMessage() within the class to modify the value internally:

class Example {
  constructor(exampleMessage) {
    this.exampleMessage = exampleMessage;
  }
  display() {
    print(this.exampleMessage);
  }
  setMessage(exampleMessage) {
    this.exampleMessage = exampleMessage;
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating an HTML file using PUG in a local environment (using npm and gulp)

Is there a way to automatically generate an HTML file with the same name as my Pug file whenever I save using gulp? I've checked all the documentation on but it only explains how to return Pug content in console... ...

Successively linking promises together within a for-each iteration

How can I ensure that a foreach loop is synchronous in AngularJS var articles = arg; articles.forEach(function(data){ var promises = [fetchImg(data), fetchUser(data)]; $q.all(promises).then(function (res) { finalData.push(res[1]); ...

Utilize Node.js to encrypt data from an extensive file

Hello, this is my initial inquiry. English isn't my native language and I need some help. I have a large file with about 800K lines that I need to read and encrypt using the sjcl library. So far, I've only managed to write the following code snip ...

Please make sure to only choose one checkbox at a time

I have designed a list-type menu using the bootstrap list group. My goal is to have only one main category active at a time. For example, if "Main Category One" is selected and then I click on another category, "Main Category Two" should become activated w ...

Navigation buttons that move the user either up or down the page

I am a beginner in programming and I wanted to create two buttons for my webpage. One button should appear when the page is scrolled more than 300px and take the user to the top, while the other button should be displayed immediately and scroll the user to ...

Updating user attributes as an administrator using the Cognito SDK

Currently, I am in the process of developing an Angular application and I aim to integrate authentication using AWS Cognito (my experience with AWS is fairly limited). So far, I have successfully incorporated features such as sign-up, sign-in, sign-out, MF ...

In TypeScript, maximize the capabilities of JavaScript modules by utilizing the import extension

Incorporating the dynamo-cache package from NPM into my TypeScript project has been a bit of a challenge. Essentially, this module introduces a new method to the AWS.DynamoDB.DocumentClient: AWS.DynamoDB.DocumentClient.prototype.configCache = function(con ...

Is it possible to enqueue a handshake after already having enqueued one in Socket IO?

Encountered a 'cannot enqueue handshake after already enqueuing a handshake' error when attempting to establish a SQL connection in Node.js. Seeking assistance in resolving this issue. Below is the code snippet that I am working with: io.on(&a ...

Triggering an AngularJS function upon window close event

Can someone help me figure out how to call an AngularJS function when a window is closing? Currently, I have the following code: app.controller("reportCtrl", function($scope, $http) { $scope.deleteReport = function() { $http.get("/SCTrakker/api/delet ...

Using Jquery to iterate through a dynamic list of elements

Currently, I am exploring the idea of creating a forEach loop that can handle an unspecified number of elements. The goal is to randomly select one element, perform XYZ actions on it, make it visible, and then eliminate it from consideration. This process ...

Refresh information once another user enters data

I'm curious about the most effective method for retrieving data when another user inserts it automatically, without requiring a page refresh. Using socket may not be the best solution since all online users would likely be in the same socket connectio ...

Tips on sending a string as an argument in an onclick function

I am currently attempting to automatically add an anchor, and here is what I have tried: " <a id='" + x.NomFic + "' onclick='TransfererFica( " +x.Idtran +" , "+ x.NumVdr + " , '" + x.NomFic ...

Searching for values using keys in Angular

Currently, I am working on a project using Angular where I need to store information based on specific identifiers. To display this information in the Angular application, I am pulling data for different identifiers and showing it on the screen. At the mo ...

Exploring the process of iterating through arrays within an object in vue.js using the v-for directive

Is there a way to iterate through an output object using the v-for template in Vue.js? new Vue({ el: app, data: { output: { player: [1, 5, 61, 98, 15, 315, 154, 65], monster: [14, 165, 113, 19, 22], }, }, }); <script src= ...

Attempting to execute an onclick event by clicking on a link is not functioning properly

Hello there, I created an HTML link <a href="javascript:void(0);" onClick="next(1)" id="next"></a> When I click on it, nothing happens. Using href="#" doesn't work either. Strangely, if I call next(1) in the console, it works. ...

From time to time, I may post files of substantial size

When moving to the next step in the form, I have implemented checks to prevent photos over 10mb and disallow .heic files from being uploaded. Most of the time it works as expected, but occasionally files slip through. If anyone has suggestions for a more ...

Mastering Number Formatting in VueJS

While working with VueJS, I encountered difficulties in formatting numbers the way I wanted. After exploring options like the builtin currency filter and vue-numeric, I realized they required modifications to achieve the desired look. Furthermore, these so ...

tutorial on adding a user-inputted value to an array in AngularJS

I need assistance with organizing my input fields that are categorized under different headings:- <label>Customer</label> <input type="text" ng-model="a.arr.customerName" name="arr[0]"/> <input type="text" ng-model="a.arr.customer ...

JavaScript event/Rails app encounters surprising outcome

I have encountered a strange bug in my JavaScript code. When I translate the page to another language by clicking on "English | Русский" using simple I18n translation, my menu buttons stop working until I reload the page. I suspect that the issue ...

JavaScript visibility disappearing intermittently

I have created a custom image viewer box that overlays a thumbnail gallery page. The image viewer should appear when a user clicks on a thumbnail. However, currently, the viewer only pops up briefly and then disappears again. I am looking for a way to mak ...