Incrementing the index in Javascript when an event occurs

I am currently working on a project that involves incrementing an index of an array based on certain events, such as a left mouse click over a designated area. The code snippet provided below initializes all values to zero and briefly changes the relevant index to 1 while the mouse is clicked, but reverts back to 0 once the mouse is released. What I aim for is to have each time the index value is incremented, it saves and retains its current value instead of resetting to 0. Ultimately, I want the array to have mixed numbers. Can anyone offer some guidance or assistance with this issue? My work is set within the Quartz Composer environment utilizing the JavaScript patch.

function (__structure out) main (__structure Pos, __boolean Left, __number X, 
          __number Y, __number W, __number H, __number ShiftX, __number ShiftY) {

    if (!_testMode) {
        len = Pos.length;
        Hits = new Array()
            for (i = 0; i < len; i++) {
                Hits[i] = 0 
            }
            for (j = 0; j < len; j++) {
                if (Pos[j][1] >= (X-(W/2)) && Pos[j][1] <= (X +(W/2)) && 
                    Pos[j][0] >= (Y-(H/2)) && Pos[j][0] <= (Y +(H/2)) && Left) { 
                Hits[j]++
                }
            }

    result = new Object();
    result.out = Hits;
    return result;
    }   
}

Answer №1

While I may not be familiar with Quartz Composer, I can certainly identify the issue in your code. Every time this event occurs, all values in the array are reset to 0.

It would be beneficial to move the following lines of code outside of your method and place them in a more global scope, either within the current class or in the true global scope.

Results = new Array()
for (j=0;j<length;j++){
    Results[j] = 0
}

Therefore, Results should be an array accessible throughout the program, and each Results[i] initialization to 0 should occur only once, at the start of execution.

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 a feature that uses a button press to initiate an Ajax request based on a specific identifier

I'm looking for the most efficient way to structure a page that involves making Ajax calls. My main issue lies in setting up the event binding for when a user clicks a button. I'm struggling to find a method to pass the ID to the function that t ...

Is it possible to update a Rails element using an AJAX request?

I've delved into a plethora of information regarding Rails, AJAX, and 5.1 Unobtrusive Javascript. It provides insight on how to handle AJAX calls in Rails with a .js file, for example. However, my goal isn't to serve up an entire .js file; rathe ...

Transferring data from a callback function within a component to a different component

I am currently utilizing the giphy-api module in Node.js and I need to transfer this data to React. Within my setup, I have two key components: The first component, Api.js, effectively connects with Node.js and returns a callback containing all of the AP ...

What is the best way to ensure a cron job executing a Node.js script can access variables stored in an .env file?

Currently, I have a scheduled job using cron that runs a Node.js script. This script utilizes the dotenv package to access API keys stored in a .env file. Running the Node.js script from the command line retrieves the variables from the .env file successf ...

Execute the laravel {{action(Controller@method}} by sending a parameter from a vue.js array

Within my view created using Blade templates, I have a link with an href attribute that directly calls a Controller method. Here is an example: <a id="@{{user.id}}" href="{{action('Controller@method')}}">>update</a> Everything wo ...

Using jQuery to append a single AJAX response at a time

I wrote a piece of code that utilizes an ajax request to communicate with json-server, retrieving data which is then displayed in an html div using jquery .html(). The content shown in the div depends on the button clicked by the user. While .append() work ...

A guide on updating URLs that are not enclosed within an anchor tag using JavaScript

In my current scenario, I am dealing with text that includes URL links presented in two different formats. www.stackoverflow.com <a href="http://www.stackoverflow.com">Stack over flow</a> My objective is to create a straightforward function ...

Combining all elements of my application into a single HTML, JS, and CSS file

My AngularJS app has a specific structure that includes different directories for each component: theprojectroot |- src | |- app | | |- index.html | | |- index.js | | |- userhome | | | |- userhome.html | | | ...

What could be causing my date variable to reset unexpectedly within my map function?

Currently, I'm utilizing a tutorial to create a custom JavaScript calendar and integrating it into a React project You can find the functional JavaScript version in this jsfiddle import { useState, useRef, useMemo } from 'react' import type ...

Tree structure implementing polymorphic relation within a multidimensional array

In order to achieve my goal, here's a straightforward explanation: I aim to create templates. Each "Template" consists of a "Panel". This "Panel" contains some "Item" and another "Panel". We need to follow this process step by step until we reach the ...

Refreshing jQuery via Ajax Response

In our JSF2 application, we encounter situations where we need to re-invoke the JavaScript (specifically jQuery for UI styling) when making Ajax calls. However, it seems that the JavaScript functions are not being called upon receiving the Ajax response fr ...

How to modify a column within a split file using Bash shell commands

I am facing an issue where a user provides the file, column, value, and id of a specific line. My goal is to update the value of that particular line. The structure of the file is as follows: F1|F2|F3|F4|F5|F6|F7|F8 My approach involves reading the fi ...

Having trouble rendering an object in my ThreeJS project. The error message says: "THREE.OBJLoader: Unexpected line: 'usemap glass'"

I encountered an error while running threejs in an angular 8 application. The objective is to load an object, for which the object and material files were obtained from Kenney assets. Despite referencing examples on the official threejs site and other onli ...

Employing setTimeout within a repetitive sequence

function displayColors() { $.each(colors, function(index) { setTimeout(function(){ revealColor(colors[index]); }, 1000); }); } I'm attempting to create a loop where the revealColor function is executed every second until all colors ...

How to Use Attributes as Component Parameters in Vue.js

I am currently developing a test Component using Vue.js. I am trying to pass a parameter to be used in my template like this: Vue.component('test', { props: ['href'], template: '<li><a href="{{href}}"><slot> ...

I'm having trouble getting $.getJSON to function properly

Has anyone encountered issues with the getJSON function in jQuery? function loadJSON(){ console.log("loading JSON") var jsonFile = themeURL+"/map.json" $.getJSON(jsonFile, function(data){ console.log("loaded JSON") $("#infobox").fadeOut(1000 ...

Enable Google Chart to visualize multiple sets of data beyond just 2

This Google chart displays 2 data sets (Tea, Coffee). I've attempted to modify it to show 5 data sets but encountered difficulties. I experimented with changing the button.onclick function and the button value. Below you will find the original code (2 ...

Identify the mouse's location in relation to its parent element, not the entire webpage

A script I've been working on detects the mouse position relative to the page, taking into account a movement threshold of 100px before triggering certain actions. // Keep track of last cursor positions var cursorDistance = 0; var lastCursorX = null; ...

The regex string parameter in node.js is not functioning properly for matching groups

The String.prototype.replace() method documentation explains how to specify a function as a parameter. Specifying a string as a parameter The replacement string can contain special patterns for inserting matched substrings, preceding and following portion ...

How does combineReducers in Redux determine which specific portion of the application state to send to the reducer?

While going through the Redux basics tutorial, I found myself a bit confused about how the code snippet below determines which part of the application state should be passed to each reducer mentioned in the combineReducers function. Does it simply rely o ...