D
D
Dymok2018-03-23 01:14:25
JavaScript
Dymok, 2018-03-23 01:14:25

How to return a variable from a function?

codepen
I'm trying to make a slider with auto scrolling. I want to implement it in such a way that there is a variable with the current slide and a function (called by setInterval) that takes this variable, changes the slide to the next one and increments the variable with the slide number by 1. The essence of the question is how to return this variable in order to use it when the next call to the setInterval function? I know about return, but I don't understand how to apply it here
. Or maybe I'm doing everything fundamentally wrong and there is an easier way? I didn't come up with anything (Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-03-23
@UnluckySerivelha

It would be more correct to take out the functions in the prototype and use the instance properties for the state:

function Slider(props) {
  /* ...  */
  this.images = props.images;
  this.currentSlide = 1;
  /* ...  */
}

Slider.prototype.changeSlide = function() {
  /* ...  */
  if (this.currentSlide === this.images.length) {
    currentSlide = 1;
  } else {
    this.currentSlide++;
  }
  /* ...  */
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question