Vue application experiencing never-ending update cycle following array assignment

Here is the JavaScript code I am working with:

const storage = new Vue({
    el: '#full-table',
    delimiters: ['[[', ']]'],
    data: {
        events: [],
        counter: 0,
    },

    methods: {
        eventCounter: function() {
            this.counter += 1;
            return this.counter;
        },
        toTime: function(raw_time) {
            console.log(raw_time)
            return moment(raw_time * 1000).format('YYYY-MM-DD HH:mm:ss');
        },
        preprocessData: function(d) {
            if (d["args"]["data"]) {
                d["data"] = d["args"]["data"];
                delete d["args"]["data"];
            }
            return d;
        },

        getData: function(query) {
            let _this = this
            $.get(events_api + 'json?' + query).done(function(new_data) {
                data = new_data.data.map(
                    (item) => _this.preprocessData(item))

                _this.events = data.slice(0, data.length);
                console.log(_this.events)
            }).fail(function(_, _, statusCode) {
                $("#error").html(statusCode);
            });
        },
    },
})
storage.getData('somequery')

This is the corresponding HTML markup:

<div id="full_table">
...
<tbody id="table_data">
<tr v-for="event in events" :key="event.time">
    <td>[[ eventCounter()     ]]</td>
    <td>[[ toTime(event.time) ]]</td>
    <td class="data">[[ event.data || '-' ]]</td>
    <td>[[ event.action || '-' ]]</td>
    <td>[[ event.desc || '-' ]]</td>
    <td>[[ event.args || '-' ]]</td>
</tr>
</tbody>
...
</div>

The issue I am facing is multiple repetitions of console.log(raw_time), displaying FirstTime and SecondTime alternately:

FirstTime
SecondTime
FirstTime
SecondTime
...

Additionally, a warning appears stating:

[Vue warn]: You may have an infinite update loop in a component render function.

I need help on how to prevent this infinite looping behavior after changing the array. Any suggestions?

Answer №1

The issue arose from modifying data within the function call inside a v-for loop in the template - specifically, the eventCounter() function.

I had a dynamic counter variable that was being incremented by 1 on each iteration of the v-for loop. This caused Vue to trigger a rerender of the page after each increment, leading to unexpected behavior.

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 custom Angular filter to group every 3 items within an iteration into a new div container

When attempting to organize three instances of app-story-preview within a wrapper div using a ngFor loop, I encountered the following code: <ng-template [ngIf]="stories.length > 0" [ngIfElse]="empty"> <cdk-virtual-scroll-viewport [itemSize ...

What steps should I take to create a horizontal list using Vue.draggable?

I am trying to drag a list of images horizontally using the draggable feature in HTML. I attempted using the direction tag, but it doesn't seem to be working. Is there another way to change the list orientation to horizontal? <div class="tier- ...

Turning JavaScript Object into a Decimal Value

I've been working on an application where I want to be able to add up the columns of an HTML table. I managed to add inputs to the table successfully, but when trying to sum up the columns, I keep getting a "NaN" error. /* Function for calculating ...

Error message encountered: React hydrate TypeError - the function __webpack_require_.i(...) is not recognized as a

I encountered a webpack TypeError while attempting to use hydrate() in my index.js file. Strangely, the error does not appear when I use ReactDOM.render() instead of hydrate. My intention behind using hydrate was for server-side rendering. src/index.js i ...

Requesting for a template literal in TypeScript:

Having some trouble with my typescript code, it is giving me an error message regarding string concatenation, const content = senderDisplay + ', '+ moment(timestamp).format('YY/MM/DD')+' at ' + moment(timestamp).format(&apo ...

Steps for converting TypeScript code to JavaScript using jQuery, without the need for extra libraries or frameworks like NPM

My single-page dashboard is quite basic, as it just displays weather updates and subway alerts. I usually refresh it on my local machine, and the structure looked like this: project/ index.html jquery-3.3.1.min.js script.js I decided to switch it t ...

Tips for dynamically adding and removing keys from a JSON object

I am working on a code for a car rental website using jQuery mobile. I need to dynamically add and remove keys from a JSON object with true or false values. The goal is to make a selected car unavailable by changing the "available" key to either true or ...

Information submitted through an ajax request does not get saved in the $_POST array

After successfully executing an AJAX request using GET, I decided to try POST this time. However, when attempting to send data, a baffling error message appeared in the console - NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED: 'JavaScript component does ...

Explore a variety of images within an array using Gatsby and Markdown documents

After spending more than 6 hours on this, I've decided to call it quits for today. However, I'm hoping someone out there might have a solution. The problem at hand is as follows: I'm trying to include an array of images in a Markdown file t ...

Adding event listener to a dynamically created video element

Could you provide any insight as to why I'm encountering an error message "Can't create event listener on null" when using the following code: var my; my.newVidObj = document.createElement('video'); my.newVidObj.src = "vid- ...

Creating a specific quantity of divs with JavaScript

For the past few hours, I've been working hard to solve this problem that has really left me stumped. As far as I can tell, everything is correct. The create function is supposed to generate a certain number of divs specified by the user in an input b ...

Discover potential routes to nodes using JavaScript

I need assistance displaying all potential node paths in JavaScript: var object = {"metadata":{"record_count":5,"page_number":1,"records_per_page":5},"records":[{"name":"Kitchen","id":666,"parent_id":0,"children":null},{"name":"Jewellery","id":667,"pare ...

Tips for storing unique values in an object using JavaScript

I need assistance with organizing my log data, which includes camera names and system IP addresses. I am trying to create an object where each unique system IP is a key, with an array of corresponding camera names as the value. Here is the code snippet I h ...

I need help figuring out how to get a horizontal scrollbar positioned at the top of a div to function properly once the content has been loaded using ajax

HTML CODE <div id="scrollidout" style="overflow: auto; overflow-y: hidden; "> <div id="scrollidin" style="padding-top: 1px; height: 20px; width: 1000px">&nbsp;</div> </div> <div class="resultcontent" style="overflow ...

Optimize your mobile experience by creating a notification menu that is scrollable and fixed in position

Recently, I purchased Moltran, but encountered a major issue: The notification menu disappears on mobile devices, which is not ideal for me. After some research, I discovered that removing the hidden-xs class from the li notification element can solve this ...

Is there a way to dynamically update text using javascript on a daily basis?

I am currently developing a script to showcase different content and images every day. While I have successfully implemented the image loop to display a new picture daily, I need assistance in achieving the same functionality for the title. My aim is to c ...

Enhance the functionality of your current JavaScript code by adding value during the change event

Looking for a way to make a javascript widget that displays a data chart interactive by allowing users to change the theme? By using a dropdown box, users can select a new theme and instantly see it applied to the widget. Check out the code below: <sc ...

The conversion of Draft-js JSON to EditorState is not functioning correctly

Recently, I utilized Draft-js to generate blog posts. When a user creates a post, the data is transformed into a string and dispatched to the server for storage. The conversion of draft-js EditorState looked like this: JSON.stringify(convertToRaw(editorSta ...

Feature exclusively displays malfunctioning image URLs for the web browser

Hello everyone! I've been diving into learning JavaScript and recently attempted to create a function that randomly selects an image from an array and displays it on the browser. Unfortunately, despite my efforts, all I see are broken link images. Her ...

Is there a way to use Laravel to send an asynchronous post request for managing likes?

Currently, I am managing likes in the following way: class LikeController extends Controller { public function like(Post $post) { $attributes = [ ['user_id', '=', auth()->user()-&g ...