0

I need to iterate a function with its argument but got the recursion error

Error in mounted hook: "InternalError: too much recursion"

the function:

methods: {
    showSlides: function(n) {
        console.log(n);
        setTimeout(this.showSlides(n), 3000);
        //setTimeout(this.showSlides(n++), 3000);
    }
},

Hama Bohel
  • 95
  • 2
  • 10
  • 2
    The first argument to `setTimeout` should be a function, not a function call. – Barmar Jun 12 '20 at 05:47
  • i used it `setTimeout(function() { this.showSlides(n++); }, 2000)` but got `TypeError: this.showSlides is not a function` – Hama Bohel Jun 12 '20 at 05:51
  • See https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback – Barmar Jun 12 '20 at 05:52
  • why did u close this question? did u check the mentioned link? – Hama Bohel Jun 12 '20 at 05:54
  • What's wrong with the link I gave? It explains why you have to use a function instead of a function call. – Barmar Jun 12 '20 at 05:58
  • as i said i also called a function inside setTimeOut but returned definition error – Hama Bohel Jun 12 '20 at 06:00
  • `setTimeout(this.showSlides.bind(this, n+1), 3000);` should do the trick. [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind) – Niet the Dark Absol Jun 12 '20 at 06:00
  • @HamaBohel That's why I gave you a second link that explains how to fix that problem. – Barmar Jun 12 '20 at 06:03
  • Thanks Niet fo your feedback, it didnt work, i dont know why that guy closed this question when there isnt a clear guid about it – Hama Bohel Jun 12 '20 at 06:04
  • try this, `methods: { showSlides(n) { setTimeout(() => { this.showSlides(n) }, 3000) } }` – LHJ Jun 12 '20 at 17:08

0 Answers0