views:

80

answers:

2

Why this code is error

  var images = ['/test/img/Gallery/large/4cba0c8a-4acc-4f4a-9d71-0a444afdf48d.jpg','/test/img/Gallery/large/4cba0ca8-2158-41af-829a-0a444afdf48d.jpg','/test/img/Gallery/large/4cbc549a-5228-433f-b0bc-0a444afdf48d.jpg'];
           $('.triggerNext').click(function(){
                  nextImage();
                  return false;
            });
            function nextImage(){
                  currentImage = $('.Pagepage:eq(0)').val();
                  nextImage = parseInt(currentImage)+1;
                  $('#imageCurrent').attr('src',images[nextImage]);
                  $('#imageCurrent') .css('position','absolute').css('left',($(window).width()- $('#imageCurrent').width() )/2);
                  $('.Pagepage').val(nextImage);
            }

it only run first time. but error on next click;

But like this it rune fine, dont error

var images = ['/test/img/Gallery/large/4cba0c8a-4acc-4f4a-9d71-0a444afdf48d.jpg','/test/img/Gallery/large/4cba0ca8-2158-41af-829a-0a444afdf48d.jpg','/test/img/Gallery/large/4cbc549a-5228-433f-b0bc-0a444afdf48d.jpg'];
           $('.triggerNext').click(function(){
                 currentImage = $('.Pagepage:eq(0)').val();
                  nextImage = parseInt(currentImage)+1;
                  $('#imageCurrent').attr('src',images[nextImage]);
                  $('#imageCurrent') .css('position','absolute').css('left',($(window).width()- $('#imageCurrent').width() )/2);
                  $('.Pagepage').val(nextImage);
                  return false;
            });

please help me. alt text

+6  A: 

It seems like your're overwritting your function here:

nextImage = parseInt(currentImage)+1;

Either change the name from your function or variable. Even better, don't use a global namespace. Anyway, you can't overwrite the name from a function, your're overwritting the function itself.

After the above line, nextImage contains a Number which obviously cannot get executed.

right from the comments

just replace nextImage = parseInt(currentImage)+1; with var nextImage = > parseInt(currentImage)+1; – Alin Purcaru

Using a var statement, also avoids the nextImage from going into the global namespace.

jAndy
+1 You have a good eye.
Alin Purcaru
Just replace `nextImage = parseInt(currentImage)+1;` with `var nextImage = parseInt(currentImage)+1;`
Alin Purcaru
this code it run on first time ...
meotimdihia
@meotimdihia: of course it will, the first time the identifier `nextImage` does contain a function (reference), which then gets overwritten by a number.
jAndy
A: 

use:

var nextImage = parseInt(currentImage)+1;

so you don't overwrite your function name

sefator