0

I'm building a PhoneGap app where I'd like to use swipe.js--which is a 'swipable' and auto-animated carousel.

To set it up, you declare it as such:

window.mySwipe = new Swipe(document.getElementById('slider'), { (parameters here) });

This is fine, and does what it should. But I'd like to set it up only on demand. Since this is in a modal, there's no reason to set it up until the modal is shown, and then if closed, I'd like it to go away as there's no reason to keep animating through the carousel when it's not visible. (Though minor, I assume it's a bit of a performance hit to have something in perpetual animation that isn't actually seen on screen)

My question is: what is the proper way set up a jQuery plugin like this on-demand as well as disable (destroy?) on-demand?

Or is this a feature that the plugin has to support in the first place?

UPDATE:

So, 'setup on-demand' is easy enough. I can simply wrap the above in a function and call that function only when needed:

function setUpSwipe(){
    window.mySwipe = new Swipe(document.etc...
}

But I'm still sumped on how to 'destroy' this after I no longer need it. I've tried 'delete':

function destroySwipe(){
    delete window.mySwipe; 
}

That does nothing (I assume because window.mySwipe is not exposed to this particular functon?)

I also tried setting the object to a blank variable:

function destroySwipe(){
    window.mySwipe = "";
}

But that was just a stab in the dark so didn't expect it to actually work. :)

DA.
  • 39,848
  • 49
  • 150
  • 213

2 Answers2

1

Looking at the source swipe.js supports a kill method that stops the timer and removes any event listeners.

dc5
  • 12,341
  • 2
  • 35
  • 47
  • Thanks, dc5! I guess that teaches me to dig into source and not always trust documentation is complete. :) So, I suppose the answer to my broad question is "you can 'reset' the plugin provided the plugin offers that ability internally". In this case, `window.mySwipe.kill()` works just great. It appears that I should be able to use `setup()` to re-set it again, but that doesn't work. So instead after I `kill()` I `delete window.mySwipe` and then when I need it again, I just call my `setUpSwipe()` that I had created earlier. Again, thanks! – DA. Jul 23 '13 at 14:49
  • 1
    It does seem to be a bit of a mixed bag when it comes to plugins. There does seem to be an attempt to standardize around a destroy message/method: [Writing Stateful Plugins with the jQuery UI Widget Factory](http://learn.jquery.com/plugins/stateful-plugins-with-widget-factory/) but it is far from ubiquitous as many (like the swipe.js library you are using) provide just enough wrapper to use with jQuery. – dc5 Jul 23 '13 at 15:47
  • yes...'stateful' that's the key term I was looking for! This info will definitely help in the future when I'm evaluating plugins. Thanks! – DA. Jul 23 '13 at 16:30
0

I used swipe.kill() it worked perfectly.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
nachi
  • 61
  • 1
  • 1