javascript - Trying to call a method within a function -
i test not defined
when calling stop() method
http://jsfiddle.net/n4mkw/1766/
<span id="rotator"></span> var rotate = { quoteindex: -1, duration: 500, delay: 3000, play: true, quotes: [], init: function (quotes) { this.quotes = quotes; this.shownextquote(); }, shownextquote: function () { this.quoteindex = (this.quoteindex + 1) % this.quotes.length; if (this.play) { $("#rotator").html(this.quotes[this.quoteindex]) .fadein(this.duration) .delay(this.delay) .fadeout(this.duration, this.shownextquote.bind(this)); } }, stop: function () { this.play = false; } }; var test = rotate.init(["example1", "example2", "example3"]); test.stop();
your functions aren't returning instance of rotate object previous answer specified. however, way can fix returning instance of object test variable.
var rotate = { quoteindex: -1, duration: 500, delay: 3000, play: true, quotes: [], init: function (quotes) { this.quotes = quotes; this.shownextquote(); return this; }, shownextquote: function () { this.quoteindex = (this.quoteindex + 1) % this.quotes.length; if (this.play) { $("#rotator").html(this.quotes[this.quoteindex]) .fadein(this.duration) .delay(this.delay) .fadeout(this.duration, this.shownextquote.bind(this)); } return this; }, stop: function () { this.play = false; return this; } }; var test = rotate.init(["example1", "example2", "example3"]); test.stop();
i have updated code return object in function. please test in fiddle.
Comments
Post a Comment