0

I am having a small problem. I want to display a slideshow, but the images are not moving. When I check it in the console I get the error

Uncaught TypeError: object is not a function

Can someone please help me?

Here's my javascript code:

        var step=1;
 var images = [
"5c.jpg",
"5s.jpg",
"a65.jpg"

];

var slideshow = document.getElementById('slideshow');

 function slide(){   
    slideshow.src = images[step];
    if(step<3){
        step++;
    }
    else
        step=1;
    setTimeout("slide()",2500); 
 }

and my html file:

<html>
    <body>
        <img src="C:\Users\M.OAN\Desktop\Pics\Slideshow\5c.jpg" alt="images" name="slide" id="slideshow" onload="slide();"/>     
    </body>
</html>
11
  • 2
    Why on earth do you have eval there? Commented May 10, 2014 at 19:55
  • why are you using eval? you don't even need it Commented May 10, 2014 at 19:55
  • Please, learn to use arrays. Do not use eval. Commented May 10, 2014 at 19:57
  • thanks for the advice but can u tell me why i m getting this error :/ Commented May 10, 2014 at 20:00
  • How do you plan on deploying this slideshow? Commented May 10, 2014 at 20:02

2 Answers 2

1

You can make Array with your image paths and then just iterate thru it:

HTML:

<img id="slideshow" src="http://placekitten.com/50/50" alt="images" name="slide"/>

JS:

var images = [
    "http://placekitten.com/100/100",
    "http://placekitten.com/100/50",
    "http://placekitten.com/50/100"
];

// get image element
var slideshow = document.getElementById('slideshow');

// in arrays index starts from 0
var step = 0;

setInterval(function(){

    slideshow.src = images[step];

    if( step < images.length-1 ){
        step++;
    } else {
        step = 0;
    }    

}, 2500 );

Demo fiddle

Btw, I recommend you to use relative paths instead of absolute, because you may have problems when you try deploy your site:

"Pics/Slideshow/5c.jpg"

instead of

"C:/Users/M.OAN/Desktop/Pics/Slideshow/5c.jpg"
Sign up to request clarification or add additional context in comments.

11 Comments

@Ejay of course not, I'm just try to solve the first problem, I've added recommendation about source too.
and i should probably tell u one thing that it was working perfectly when i was calling slide mthod from onload event in body tag but i had to remove it from there for some reasons
@user3624126 Try fiddle demo that I've inserted above
it works but i m having the same problem i suffered from when i used onload in body tag, the slideshow starts right at the header but i want the image src to change in the image tag , that's why i specifically called the onload in img tag so the images will be changed there not in header
@user3624126 I'm not sure that I understand exactly what you try to explain, but if you want to get specific element in DOM, just use document.getElementById('elementID') function.
|
0

From Fiddling a bit, the source of the original error was that the name of the tag and the function were the same.

1 Comment

you should post it in 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.