Timing interval in Vue for setting and clearing intervals

My Vue.js image slider has been set up to loop through 4 images using a setInterval function. It's almost perfect, but I've noticed that there is a delay when the last image is reached and it resets back to the first image. Is there a way to make this transition smoother and quicker?

<script>
export default {
  name: "Slider",
  data() {
    return {
      currentSliderIndex: 0
    };
  },
  mounted() {
    setInterval(() => {
      this.currentSliderIndex = this.currentSliderIndex + 1;

      if (this.currentSliderIndex > 4) {
        clearInterval();
        this.currentSliderIndex = 0;
      }
    }, 4000);
  }
};
</script>

Answer №1

There is no need to utilize clearInterval in this scenario since you are not providing the interval ID, as highlighted by Peter Wolf in the comments.

The issue lies in your condition for resetting the currentSliderIndex. Instead of checking if currentSliderIndex > 4, you should check if currentSliderIndex > 3 because index 4 corresponds to the 5th element in your image array. Update your code like so:

<script>
export default {
  name: "Slider",
  data() {
    return {
      currentSliderIndex: 0
    };
  },
  mounted() {
    setInterval(() => {
      this.currentSliderIndex = this.currentSliderIndex + 1;

      if (this.currentSliderIndex > 3) {
        this.currentSliderIndex = 0;
      }
    }, 4000);
  }
};
</script>

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

Is there a way for me to connect the ajax data to Vue components?

Utilizing Jquery ajax to load data from an external API has been successful and the v-for text is also working without any issues. Vue var vm = new Vue({ el:'.inlinePoetry', data:{ PoetryList:[] }, created:function(){ var ...

Import the .obj file along with its original color information, without the need for the accompanying

I am facing a challenge with a .obj file that appears colored in software like MeshLab or Microsoft's 3D builder. However, there is no .mtl file linked to it. When I try to open it in ThreeJs using the most basic method, it shows up as grey. var load ...

Adjust the CSS when Vue 3 dynamically loads a new image

Is it possible to dynamically change the CSS class of a div depending on whether or not an image has been successfully loaded? The image source is obtained from an input field, allowing for the possibility of changing the <img src>. I've been ab ...

"Exploring the usual progress of a standard GET request using Axios

My Objective: I am utilizing Vue And Axios, with the goal of displaying the progress in percentage on the console. The Challenge: The request itself takes around 4 seconds or more because it fetches a large amount of data, processes it into an excel fil ...

What are the steps for conducting a component test with material ui?

My current component is built using . import React from 'react'; import { AppBar, Toolbar } from 'material-ui'; import { Typography } from 'material-ui'; import { MuiThemeProvider, createMuiTheme } from 'material-ui/sty ...

Utilizing the Pub/Sub architecture to integrate the kafka-node library within Node Js

Utilizing the kafka-node module in my NodeJs Microservise project, I am aiming to implement a Pub/Sub (publisher and subscriber) design pattern within the Functional programming paradigm. producer.js const client = new kafka.KafkaClient({ kafkaHost: ...

Please insert a decimal point and thousand separator into the text field

I'm trying to incorporate thousand separators and decimal points into my text box. Additionally, I have implemented the following directive: .directive('format', function ($filter) { 'use strict'; return { requir ...

Facing issues with debugging JavaScript using WebStorm lately

Previously, I had no issues debugging JavaScript from WebStorm with the help of the JetBrains Chrome extension. It would effortlessly open a new tab in Chrome and launch my site without any trouble. However, after rebooting my computer due to a Windows up ...

Acquiring data from a JavaScript file in JSON format with JINT

Recently, I received a JavaScript file that contains two JSON objects. The first object stores different languages, while the second object contains various labels. var languages = {"Languages":["English","Cymraeg","Deutsch"]}; var labels = [{"$JOB":["Job ...

Steps for setting up i18nextStart by including the i

I am working on developing a multilingual app using the i18next package. Unfortunately, I am experiencing issues with the functionality of the package. Below is an example of the i18next file I have been using: import i18n from "i18next"; impor ...

Iterate over an array utilizing the $.getJSON method for data retrieval

I have encountered an issue while using a for loop to iterate through an array of dates in a JSON request. The problem is that the loop seems to be fetching only the first item in the array each time it iterates, as if ignoring the variable i or being cach ...

how to toggle a pre-selected checkbox in a Vue component

https://i.stack.imgur.com/6GQl7.png When dealing with a tree view checkbox in Vue, I encountered an issue where editing data within a selected zone should automatically check the corresponding countries based on previous selections. The ID of the selected ...

Refreshing the connected value of an input upon making changes

I'm currently developing a simple to-do application. Each task is included in an input item within a Vue component called <list-item>. These <list-item> components are generated using a v-for directive that points to an array of tasks. Th ...

Develop a wrapper for API handlers to minimize redundancy

Currently, I find myself having to duplicate this code in every component whenever I need to call a private endpoint. This approach is messy, not sustainable, and prone to errors. How can I encapsulate this behavior within a function that I can place in ...

Vue Django application access denied: CSRF verification failed

It seems like I have encountered a Django issue with the backend system. My Vue code is located in frontend/ (127.0.0.1:8080) while the Django code resides in backend/ (127.0.0.1:8000). I have carefully followed the instructions provided by Django regardin ...

Showing the loading screen while waiting for the static Next.js app to load

Looking for a way to implement a loading screen right before the entire static page finishes loading? I'm currently utilizing modules:export to create my static page, but struggling to listen to the window load event since NextJs has already loaded th ...

Is there a way to initiate the server only after webpack has finished bundling all of the bundles

"scripts": { "start": "node server.js", "build": "webpack" }, Is there a way to execute both npm run build and npm start with a single command? "scripts": { "start": " ...

troubleshooting color problem with video textures in three.js

There seems to be a slight variation in the colors of my video texture compared to the original video. I've experimented with different three.js encoding options, but I still notice this discrepancy. Does anyone have any tips on how to avoid this is ...

The detected coordinates are offset from the location of the mouse click

Seeking guidance: I need advice on an issue that arises when clicking on the second tooth from right to left, causing the upper teeth to be colored instead: https://i.sstatic.net/czzmc.png Below is a step-by-step explanation of what the code does: 1) T ...

Directing users to a specific section on another webpage can be accomplished using HTML, JavaScript, or

<nav> <div class='container-fluid'> <h1 class='logo'>Logo</h1> <ul class='list-unstyled naving'> <li><a href='index.html'>Home</a></li> ...