0

I'm trying to write a script that displays a series of client testimonials, toggling the visibility of each one in order. I'm trying to use a queue to do this, and was told to use a regular Javascript array, which has the methods push() and shift() that enable queue functionality. However, when I try to pass the array into the function I wrote, I get this error:

TypeError: q.shift is not a function

So, somehow, the array I'm passing into the function is no longer an array somehow. Can someone more experienced with Javascript explain why this is happening?

Code:

function testimonials(q) {
       var e = q.shift();

       jQuery(e).fadeToggle("slow", testimonials(q.push(e)));

}

jQuery(document).ready(function() {

    var elements = jQuery(".fade-text").toArray();

    var queue = [];

    // add to queue
    for (i = 0; i  < elements.length; i++) {
        queue.push(elements[i]);
    }

    testimonials(queue);
});
5
  • what is it if you do console.log(q)? Commented May 3, 2016 at 17:06
  • It gives me the array, so it is getting passed into the function. Commented May 3, 2016 at 17:07
  • See if queue is set immediately before you invoke testimonials(queue). Also see if elements is set correctly. Commented May 3, 2016 at 17:09
  • what is the output if you do console.log(queue); right above testimonials(queue); and console.log(q); right above var e = q.shift();? Commented May 3, 2016 at 17:09
  • The issue appears to be testimonials(q.push(e)), since push does not return an array, rather the new length of the array. Commented May 3, 2016 at 17:13

2 Answers 2

5

Here is the problem testimonials(q.push(e)), the return value of push is a number and that doesn't have a method called shift().

 jQuery(e).fadeToggle("slow",function(){ testimonials((q.push(e), q)) });

Also you are not using the callBack of fadeToggle properly, you are calling the function testimonials immediately. Wrap that function call in an anonymous function and pass it as a callBack.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

jQuery(e).fadeToggle("slow",function(){ testimonials((q.push(e), q)) });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.