JavaScript has encountered an error due to exceeding the maximum call stack size, resulting in an un

I've been working on a project where I wanted to recreate the retro DVD logo bouncing around the screen but with a custom picture instead. However, I keep encountering an error message that's making it quite frustrating to work through.

Here is the snippet of my code:

<img id="image" onclick="main()" onmouseover="imgRotation()" onmouseout="backNormal()" src=*image source* alt="Custom Logo">

<canvas id="tv-screen"></canvas>

And here is my JavaScript code:

var dvd = {
  x: 200,
  y: 300,
  xspeed: 10,
  yspeed: 10,
  img: document.getElementById("image")
}
var canvas = document.getElementById("tv-screen");
var speed = 20;
var scale = 0.50;

function main() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    update();
}

function update() {
    dvd.x += dvd.xspeed;
    dvd.y += dvd.yspeed;
    checkHitBox();
    update();
}

function checkHitBox() {
    if (dvd.x + dvd.img.width * scale >= canvas.width || dvd.x <= 0) {
        dvd.xspeed *= -1;
    }

    if (dvd.y + dvd.img.height * scale >= canvas.height || dvd.y <= 0) {
        dvd.yspeed *= -1;
    }
}

Answer №1

It appears that there is a recursive call in your update function.

function update() {
        dvd.x+=dvd.xspeed
        dvd.y+=dvd.yspeed
        checkHitBox();
        update(); // recursive call
    }

Have you considered implementing a timeout instead?

function update() {
        dvd.x+=dvd.xspeed
        dvd.y+=dvd.yspeed
        checkHitBox();
        setTimeout(update, 100) // every 100ms
    }

Alternatively, you could opt for using a fixed interval:

 function update() {
        dvd.x+=dvd.xspeed
        dvd.y+=dvd.yspeed
        checkHitBox();
    }
setInterval(update, 100);

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

Unable to modify an element within an array using findIndex

Struggling with updating a value within an array, my research has not yielded a working solution. I have encountered multiple issues. Array = [{ val1: "1", val2: "2", nameval: "name", val4: "3" },{ val1: "4", val2: "5", nameval: "name", val4: ...

toggle back and forth between two div elements

I am trying to create a toggle effect between two divs, where clicking on one will change its border and header color to red while the other div disappears. I have tried using an IF statement in my JavaScript code, but it is not working as expected. Can so ...

The jQuery AJAX function successfully executes, but the MySQL post deletion operation fails to take place

I am encountering an issue with this particular code. The Ajax code runs through to the end and then fades out the parent of the delete button. Below is the code for the delete button, post, and Ajax: <?php include('php/connect.php'); ...

Accessing data points in jQuery

Here is a sample code I'm working with : <script type="text/javascript"> var currentPicture;//default picture var picEL;//the image viewer element jQuery("#backImageShower").hover( function(i) { picEL = j ...

JSReports encountered an unexpected token "<" in the JSON at position 0

Seeking assistance from those knowledgeable in JSReports, but open to suggestions from all... I've investigated the common issue of "unexpected token < in JSON at position 0", which typically arises when attempting to parse an HTML-formatted strin ...

Unable to access the newly created object's properties following the instantiation of a new resource in AngularJS

Currently, I am in the process of developing a new Resource utilizing AngularJS that falls under the category of Person. After successfully creating this resource, my goal is to retrieve the id associated with the new resource from the server. it('sh ...

Managing promise chain within the constructor of a React component

I am experiencing an issue with my API data fetching process. The challenge occurs because the rendering takes place before the constructor has completed fetching the data. As a result, the this.todo being sent to the List component ends up being empty. ...

The initial rendering of the connected component is unsuccessful due to the fact that the Redux state has not been fully

Currently, I am utilizing Redux thunk and axios to handle server requests and update the state based on the response. An issue arises when trying to render a connected component with an initial state that relies on data from the server. In this scenario, ...

Error in React Bootstrap: ReferenceError - 'openModal' is not defined

Whenever the button is clicked, my intention is for a modal to open. I have written the code for it, but I'm encountering errors that I can't seem to resolve. Here's the snippet of my code: import React from "react"; import { M ...

Simulate internationalization for vue using jest

Currently, I am working on setting up jest unit tests for a Vue project within a complex custom monorepo. I am facing an issue with i18n, which I use for translation management in my application. The problem arises with the following code snippet for init ...

Tips on passing string without double quotes within a MongoDB query

When trying to pass a string to my mongodb query, I encountered an issue where double quotes were added to the string once inside the query. The criteria is being passed as a string because it is dynamically formed that way: str1={AccId:8,WSId:24237,Case ...

React: Conceal additional elements once there are over three elements in each section

React: I am trying to create a menu with submenus. My goal is to calculate the number of submenus and if it goes over 3, hide the additional submenus. Below is the code snippet for counting the elements: export default function App() { const ElementRef ...

Is it necessary to release a new library version for non-functional updates?

As the maintainer of several JavaScript libraries, I often find myself needing to update dependencies that don't necessarily require any functional changes. This is especially true when my library is not impacted by a breaking change in one of its dep ...

What is the best way to retain multiple values passed through Output() and EventEmitter() in Angular?

In my Angular application, I have implemented custom outputs to transmit user-selected values between components. Currently, the functionality allows for the selected value from checkbox items to be sent to a sub-component, where it is displayed in the con ...

Tips for removing a previous file from an 'input type' in HTML5 FileReader

Here is a file input with an associated function: <img id="uploadPreview"> <div id="changeImage">Change</div> <div id="customImage"> <input type="file" id="myfile" multiple style="display:none" onchange="PreviewImage();" / ...

Using Fabric JS to update the image source of an element with a new URL

Can the src attribute of a fabric js object be changed to a CDN url? { version: '4.4.0', objects: [ { type: 'image', version: '4.4.0', originX: 'left', ... src:'www.cdn ...

Refreshing AngularJS: How to reload Ngtable with previous data

Currently, I am utilizing NGTable to exhibit data on my webpage. The data is stored in an array and passed through the dataset parameter. However, when I dynamically modify a value in the array, the changes are accurately reflected in the table BUT if I na ...

Can the server-side manipulate the browser's address bar?

Picture this scenario: a public display showcasing a browser viewing a web page. Can you send a GET or POST request from a mobile device to an HTTP server, causing an AJAX/pubsub/websocket JavaScript function to alter the displayed page on the screen? Per ...

Saving the subtracted value from a span into a cookie until the checkbox is unchecked - here's how to

I am working on a piece of code that includes numeric values within a span. When the checkbox is clicked, it subtracts 1 from the main value, essentially reducing the total value. How can I achieve this so that even after the form is submitted, the deducte ...

Do we always need to incorporate components in Vue.js, even if there are no plans for reuse?

I've been pondering this question for some time now: is it necessary for every component to be reusable? Consider a scenario where we have HTML, CSS, and JavaScript that cannot be reused. For instance, a CRUD table designed specifically for managing u ...