ganlanyuan / tiny-slider

Vanilla javascript slider for all purposes.
MIT License
5.23k stars 785 forks source link

Cannot read property 'length' of null #367

Open k-ivan opened 5 years ago

k-ivan commented 5 years ago

Hi, thanks for the plugin. Issue description: I encountered an error when destroing a plugin on a specific breakpoint. An error appears if the window is resized immediately after launch destroy method. Apparently, the variable slideItems at this point has already been reset, and the forEach is trying to use it.

setTimeout(function() {
  forEach(slideItems, function(el) {
    removeClass(el, 'tns-moving');
  });
}, 300);

Demo link/slider setting: Example

Tiny-slider version: 2.9.1 Browser name && version: Google Chrome 71.0.3578.98 OS name && version: Windows 10

Rebelfront commented 5 years ago

I have also an issue with the length:

"common.js:25759 Uncaught TypeError: Cannot read property 'length' of undefined".

My console tells me taht the problem comes from here in the slider js source file:

// make sure at least 1 slide if (options.container.children.length < 1) { if (supportConsoleWarn) { console.warn('No slides found in', options.container); } return; }

r4fx commented 5 years ago

the same for me, after resizing window:

forEach.js:3 Uncaught TypeError: Cannot read property 'length' of null
    at forEach (forEach.js:3)
    at tiny-slider.js:1956
forEach @ forEach.js:3
(anonymous) @ tiny-slider.js:1956
setTimeout (async)
updateGallerySlidePositions @ tiny-slider.js:1955
resizeTasks @ tiny-slider.js:1444
(anonymous) @ tiny-slider.js:1212
requestAnimationFrame (async)
onResize @ tiny-slider.js:1212
jaclyntan commented 5 years ago

This error occurs because the script is trying to create a slider out of an element that likely doesn't exist on your page. I am using this script for the first time and came across this too.

You need to do a check to see if the slider element exists first if you're running the script across your entire site

eg.

if ( $('.feature.slider').length > 0 ) {
    var slider = tns({
    //options here
    });
}

I feel like this is something that should be handled by the script itself though, hopefully it will be updated in the future.

bekahmcdonald commented 4 years ago

This error occurs because the script is trying to create a slider out of an element that likely doesn't exist on your page. I am using this script for the first time and came across this too.

You need to do a check to see if the slider element exists first if you're running the script across your entire site

eg.

if ( $('.feature.slider').length > 0 ) {
    var slider = tns({
    //options here
    });
}

I feel like this is something that should be handled by the script itself though, hopefully it will be updated in the future.

Seconded. It'd be great if it could handle the slider not being present yet, or being empty.

momenfc commented 2 years ago

if use react put tns in useEffect like that

useEffect(() => { tns({ container: '.slider-list', items: 1, slideBy: 'page', autoplay: true, }) }, [])