What are some solutions for troubleshooting setInterval issues?

I have a h1 element with a v-for loop that displays items from my array in the following format:

 <h1
        v-for="(record, index) of filteredRecords"
        :key="index"
        :record="record"
        :class="getActiveClass(record, index)"
      >
        <div :class="getClass(record)">
         
          <strong v-show="record.path === 'leftoFront'"
            >{{ record.description }}
          </strong>
          
        </div>
      </h1>

In the code above, I am binding a class (getActiveClass(record,index)) which takes a record and an index as parameters.

This is what my getActiveClass method looks like:

getActiveClass(record, index) {
      this.showMe(record);

      return {
        "is-active": index == this.activeSpan
      };
    }

The showMe method is used for setInterval to switch between records based on their time intervals. Here's how it works:

 showMe(record) {
     console.log(record.time)
      setInterval(record => {

        if (this.activeSpan === this.filteredRecords.length - 1) {
          this.activeSpan = 0;
        } else {
          this.activeSpan++;
        }
      }, record.time );
    },

The activeSpan variable ensures the correct switching of the 'is-active' class as shown in the code snippet above.

However, I am facing an issue where the record.time values are not functioning correctly. When printing them out, both times are logged instead of changing at the specified intervals. This results in a fast looping through the records rather than displaying each record for its designated time duration.

How can I resolve this issue and set up the intervals so that each record is displayed according to its specific time value?

FOR EXAMPLE :

filteredRecords:[
{
description:"hey you",
time:12,
id:4,
},
{
description:"hola babe",
time:43,
id:1
},

]

For instance, "hey you" should be displayed for 12 seconds, followed by "hola babe" for 43 seconds.

Thank you.

Answer №1

<template>
  <h1 ...>{{ filteredRecords[index].description }}</h1>
</template>

<script>
{
  data() {
    return {
      index: 0,
      // ...
    };
  },

  methods: {
    loopThroughData(i) {
      if (this.filteredRecords[i]) {
        this.index = i;
        window.setTimeout(() => loopThroughData(i + 1), this.filteredRecords[i].time * 1000);
      }
    },
  },

  mounted() {
    this.loopThroughData(0);
  },
}
</script>

How about implementing a looping mechanism without the use of v-for directive?

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

Send information to a Google form using AJAX requests

I have encountered an issue while attempting to send data via AJAX to a Google form. Despite receiving a success message with a statusCode of 0, the data does not show up in the form: var dat={ "entry.529474552" :"data1", "entry.1066559787": "name1"}; pos ...

Comparing SHA and Python hashlib outputs show discrepancies with identical inputs

These code snippets both use Nodejs and Python to calculate a hash from the same input content, however they seem to be generating different results which is quite puzzling. // npm install jssha const jssha = require("jssha"); var s = new jssha(& ...

Minimizing conditional statements in my JavaScript code

I've just completed the development of a slider and am currently working on optimizing some repetitive if/else statements in the code. The purpose of these conditions is to determine whether the slider has reached the last slide, in which case the nex ...

send back information obtained via an ajax request made by a javascript module

Here is a code snippet featuring an object with a function that makes an AJAX call. The function currently returns an empty array, but we need to figure out how to return the data after receiving the response. var receivedData = []; var AjaxUtil = { ...

What is the best way to show one slot at a time?

As I embarked on the journey of building my unique carousel, I made the strategic decision to utilize slots for this endeavor. I have developed two essential components: a carousel.vue and slide.vue. The carousel.vue component carefully organizes all slid ...

Parent element with 'overflow: hidden' in CSS is causing child styles to be cropped out

.util-truncate { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } a { text-decoration: none; } a:focus { box-shadow: 0 0 0 3px blue; text-decoration: underline; } a:focus-visible { outline: 0; } <div ...

Is it possible to generate a Token in Nexus for private packages without using the UI interface?

We have implemented Sonatype Nexus Repository ManagerOSS 3.29.0-02 and are currently facing an issue in generating a TOKEN that can be used with .npmrc following this specific structure: registry=http://NEXUS-IP:8081/repository/GROUP-NAME http://NEXUS-IP:8 ...

Unable to use href attribute as intended

HTML: <a id="Switch">Click here to switch</a> <a href="image1_large.png" class="mainA blade"> <img id="mainImage" src="image1.png"/></a> Javascript: <script> $(function() { $('#Switch').click(functio ...

How can I attach a click event to the left border of a div using jQuery?

I am wondering about a div element <div id="preview"> </div> Can anyone suggest a method to attach a click event specifically to the left border of this div? Your help is greatly appreciated. ...

The overlappingmarkerspidifier is throwing an error because it is unable to access the property '__e3_' of an undefined variable

I am attempting to implement the overlapping marker spidifier feature in my code. I followed the instructions provided in this link: functioning code of oms However, upon creating the OverlappingMarkerSpiderfier object, I encounter the error: Uncaught Typ ...

nodemon breaks down frequently while anticipating changes in files

After cloning a project that I finished 2 months ago, I am facing an issue where nodemon won't run. Despite trying to close npm using task manager on Windows and running it again, the error persists. I am also utilizing MongoDB as my database. If any ...

The best practices for coding Jquery plugins to adhere to style guidelines

I have been grappling with this issue for quite some time now, and I am seeking feedback from others. I am in the process of creating a personalized library to expedite web app development, and I want to ensure that I adopt the correct approach. My intenti ...

Ways to retrieve an array following a function call

After a lot of testing and troubleshooting, I finally got my array to function properly in the console. However, when I click the button, the array is displayed on the console but not in my HTML. TS: jogar(){ for(var u=0;u<6;u++){ this.y ...

Error message: "PHP encounters an issue with jQuery due to absence of 'Access-Control-Allow-Origin' header"

I am currently attempting to make an external API call in PHP using AJAX and jQuery, but I keep encountering the error message saying "No 'Access-Control-Allow-Origin' header is present". The API does not support JSONP. Is there any workaround t ...

What is the best way to adjust the size of an image within a div element using HTML?

I've created a Carousel with 5 images to display. However, I'm having an issue where only the image is showing up, and I want the text and button to appear below it. Here's how the nature image is currently displaying: What I want is for th ...

Guide on integrating React with Node.js and HTML

I've tried various methods, searched on Google, and checked the official site, but nothing seems to work. The code only functions properly when I include it in the HTML file of node.js like this: <!DOCTYPE html> <html lang="en"& ...

What is the best way to utilize a single npm module in multiple TypeScript files?

Question: I keep encountering the error message "error TS2451: Cannot redeclare block-scoped variable 'os'" when I try to import the same npm module in multiple TypeScript files and run the TypeScript compiler tsc. Here is an overview of my proj ...

Determining html column values using a related column and user input

Is there a way to populate an HTML table column (using Javascript or jQuery) based on the values in another column and an input field? For instance, if I input the number 115 into the field, then the advance column should display a value of 1 for each ath ...

Oops! We couldn't locate or access the file you're trying to import: ~bootstrap/scss/bootstrap

Following the steps outlined on getbootstrap.com, I assumed that everything would function smoothly. Unfortunately, that hasn't been the case so far. All seems well until I attempt to load the page, at which point my Express.js app throws a: [[ ...

Is it possible for me to load a window following a click

I developed a customized Modal Box that functions similar to the browser's "alert()". When using the traditional alert(), it halts the rendering and executions of the underlying webpage. I am seeking methods to achieve this same behavior: preventing ...