Nicholas Moore
Nicholas Moore

Reputation: 172

Simple Javascript Slideshow from Database Array

I have to create a slideshow out of image paths that I store in a database and create the array using PHP, calling it with the JQuery ajax function. I can successfully call the array, however I am having trouble getting the Javascript slideshow to work. Here is my script:

window.onload = slideshow(0);

var i;

function slideshow(dir) {

var d = dir;
i =+ d;

$.ajax({
    url: "comiccheck.php",
    datatype: "json",
    success: function(data, textStatus, xhr) {
        data = JSON.parse(xhr.responseText);
        if(i < 0) {
            i = data.length - 1;
        }
        var comic = data[i];
        $('#comic').replaceWith("<img id='comic' src='" + comic + "' alt='comic' />");  
    }
});
}

The function slideshow(dir) is being called in the HTML when the user clicks on an arrow button, returning 1 or -1, depending on the direction. Right now the buttons will only advance the image once in each direction, but not any further. Please let me know of any questions or comments you might have on the issue, thank you.

Upvotes: 0

Views: 588

Answers (2)

Alexis Pigeon
Alexis Pigeon

Reputation: 7512

Is the i =+ d intended? Shouldn't it be i += d ?

Upvotes: 4

Yehonatan
Yehonatan

Reputation: 3225

Try puting the window.onload = slideshow(0); after the function declartaion, you can't call a function before you declared it.

Upvotes: -2

Related Questions