更改方向时无法清除间隔或超时



我有一个带有Swipejs的旋转木马,我在从肖像 ->景观或风景 ->肖像中更改方向时销毁然后重建。

我有一个问题,我似乎无法通过下面的代码清除间隔/超时。正如我所看到的那样,console.log(now)开始每5秒(预期)每2.5秒被调用一次。大概这是因为初始setInterval功能仍然处于活动状态。

我如何清除/杀死它,以便任何一次间隔都可以工作?

我正在使用jQuery Mobile 1.3.1。

if ($.event.special.orientationchange.orientation() === 'portrait') {
    // Portrait mode
    clearTimeout(slideshowHandle);
    // ...trash the old slider, then build a new one...
} else {
    // Landscape mode
    clearTimeout(slideshowHandle);
    // ...trash the old slider, then build a new one...
}
// call the SwipeJS next method every 5 seconds to move to the next slide in the carousel
var slideshowHandle = setInterval(function() {
  // just some code for debugging purposes
  var now = new Date(),
  now = now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();
  console.log(now);
  // trigger a swipe 'next'
  container.Swipe('next');
}, 5000);

您应该使用clearInterval(slideshowHandle)代替clearTimout

检查此信息是否参考https://developer.mozilla.org/en-us/docs/web/api/window.clearinterval

我一定有一个范围范围的问题,因为它将其附加到window完成了技巧(并且也更改回clearInterval):

// clear the interval on the slideshow 'next' (if we have it) when changing orientation
if (window.slideshowHandle) {
   window.clearInterval(window.slideshowHandle);
}
// call the SwipeJS next method every 5 seconds to move to the next slide in the carousel
window.slideshowHandle = window.setInterval(function() {
   swipeContainer.Swipe('next');
}, 5000);

最新更新