0

I've got a really simply jquery plugin that I'm just trying to read the options from but can't figure out what I'm doing wrong. When I try to run this I get an "options is undefined js error."

Here is my code:

(function($) { 

$.fn.extend({
headerSlideshow: function(){

    var defaults = {
            padding: 20,
            mouseOverColor : '#000000',
            mouseOutColor : '#ffffff'
        }

    var options =  $.extend(defaults, options);

},
header: function() {
    this.headerSlideshow(options);
    setInterval( "this.headerSlideshow(options)", 1000 );
}
});

}) (jQuery);  
Delmon Young
  • 2,013
  • 5
  • 39
  • 51
  • have you passed any options in calling script ? and you need to provide options parameter in `headSlideshow` function – exexzian Jul 25 '13 at 15:42

1 Answers1

2

Most likely you have to accept an options parameter in your headerSlideshow and header functions:

function(options)

Also, don't pass a string to setInterval. This will use eval. Better pass a function:

header: function(options) {
    var element = this;
    this.headerSlideshow(options);
    setInterval(function() { element.headerSlideshow(options); }, 1000);
}
Dennis
  • 14,210
  • 2
  • 34
  • 54
  • `this` inside the `setInterval` is *not* what you think it is. You need to cache `this`. – gen_Eric Jul 25 '13 at 15:42
  • this works perfect thanks @Chips_100 but why would I pass a function? couldn't i run this: element.headerSlideshow(options); – Delmon Young Jul 25 '13 at 18:15
  • 1
    I am not sure if I understood you correctly. In your question, your first parameter to `setInterval` is a string, containing code. This should be avoided, as that calls `eval` under the hood, to evaluate that code expression. see http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea instead, it is better to pass an anonymous function to `setInterval` which contains the code to run. – Dennis Jul 25 '13 at 18:18