1

I have this jQuery Plugin that looks like this:

(function($){ 
$.fn.s3Slider = function(vars) { 
 // (...)
 timeOutFn = setTimeout(makeSlider, thisTimeOut);
 // (...)
 var makeSlider = function() {
 // next Image
 }
 makeSlider();
}; 
})(jQuery);

I can start it with

jQuery(document).ready(function() { 
 jQuery('#s3slider').s3Slider({
 timeOut: 4000
 });
});

Now my question, how can I execute the makeSlider() function from outside? It's an Image Slider and I want to add a Next Button function.

I want something like this, but it's wrong

jQuery.s3Slider.makeSlider();
Andy G
19.4k5 gold badges49 silver badges70 bronze badges
asked Aug 26, 2013 at 19:09
1
  • @Neal sorry didnt realize he wanted it OUTSIDE of the function Commented Aug 26, 2013 at 19:13

4 Answers 4

2

You can return an object containing references to the functions you want to expose:

(function($){ 
 $.fn.s3Slider = function(vars) { 
 ...
 function next(){
 // Advance the slide here
 }
 // Return only the functions that you want to expose
 return {
 next: next
 };
 }
}; 
})(jQuery);

Then you can use it like:

var s3Slider = jQuery('#s3slider').s3Slider({
 timeOut: 4000
});
s3Slider.next();
answered Aug 26, 2013 at 19:12
Sign up to request clarification or add additional context in comments.

1 Comment

I think that's the easiest solution. Thank you for this, it works:)
0
$.fn.s3Slider = function(...) {
 ...
 return { makeSlider: makeSlider };
}

Comments

0

Currently makeSlider only exists inside the scope of the s3Slider function.

Why not make makeSlider it's own plugin, then have s3Slider (and you) call that?

(function($){
 $.fn.makeSlider = function(){
 // Next image
 };
 $.fn.s3Slider = function(vars){
 // Your code
 var that = this;
 setTimeout(function(){
 $(that).makeSlider();
 }, thisTimeOut);
 };
})(jQuery);
answered Aug 26, 2013 at 19:13

Comments

0

You can achieve what you want but it's a little more complex.

Change

$.fn.s3Slider = function(vars) { 
 // (...)
 timeOutFn = setTimeout(makeSlider, thisTimeOut);
 // (...)
 var makeSlider = function() {
 // next Image
 }
 makeSlider();
};

to

$.fn.s3Slider = (function(){
 var makeSlider;
 var f = function(vars) { 
 // (...)
 timeOutFn = setTimeout(makeSlider, thisTimeOut);
 // (...)
 makeSlider = function() {
 // next Image
 }
 makeSlider();
 }; 
 f.makeSlider = makeSlider;
 return f;
})();

After that, you can both use the s3Slider function as

$(someElement).s3Slider()

and do

$.fn.s3Slider.makeSlider();

or

$(someElement).s3Slider.makeSlider();

to access makeSlider from outside.

answered Aug 26, 2013 at 19:14

1 Comment

s3Slider is a property of $.fn (which is a alias to jQuery.prototype). jQuery.s3Slider is not defined in your answer. You'd have to do $(someElement).s3Slider().makeSlider() (or $.fn.s3Slider.makeSlider();).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.