swiper icon indicating copy to clipboard operation
swiper copied to clipboard

"Cannot set properties of undefined (setting 'running')" in case of starting autoplay for destroyed Swiper instance

Open andrew-demb opened this issue 5 months ago • 0 comments

Check that this is really a bug

  • [x] I confirm

Reproduction link

n/a

Bug description

We have a code like this

if (!swiperRef?.current) return;

swiperRef.current?.autoplay?.start();

Which is called after rendering the Swiper (after some hardcoded time - 2 seconds in our case)

When the user opens the website, there is an error in the browser:

91946-01c64a790a6fd4ee.js:10 Uncaught TypeError: Cannot set properties of undefined (setting 'running')
    at Object._ [as start] (76019-f1ab80b847405ee8.js:1:185102)
    at 94079-76b72bc35f7df08c.js:1:16416
    at n (91946-01c64a790a6fd4ee.js:10:3643)

After investigation, we've found that the error occurred in the case of calling autostart.start() for a Swiper instance that was already marked as destroyed.

And after checking the Swiper's code - it seems like that check for destroyed state is done for some other autoplay methods, but not for start().

(no check for destroyed state - start()) https://github.com/nolimits4web/swiper/blob/8df837e63735c088e9b8cc6dfc2a6be38d6900e7/src/modules/autoplay/autoplay.mjs#L141-L146

(check for destroyed state is presented - pause()) https://github.com/nolimits4web/swiper/blob/8df837e63735c088e9b8cc6dfc2a6be38d6900e7/src/modules/autoplay/autoplay.mjs#L154-L160

Image

Image

Expected Behavior

Calling start() method for Swiper instance in destroyed state doesn't lead to JS error

Actual Behavior

Uncaught TypeError: Cannot set properties of undefined (setting 'running')
    at Object._ [as start] 
...

Swiper version

11.1.1

Platform/Target and Browser Versions

Google Chrome

Validations

  • [x] Follow our Code of Conduct
  • [x] Read the docs.
  • [x] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
  • [x] Make sure this is a Swiper issue and not a framework-specific issue

Would you like to open a PR for this bug?

  • [x] I'm willing to open a PR

andrew-demb avatar Jul 01 '25 13:07 andrew-demb