有没有办法暂停循环,直到用户向右或向左滑动?

时间:2016-04-23 03:18:12

标签: javascript jquery swipe

我需要一种简单的方法来暂停我的while循环,直到用户向左滑动。现在,我的while循环遍历所有对象而没有给它们显示的机会,因此只有最后一个对象最终显示。我正在寻找的是让我的函数运行一次并等到用户再次向右或向左滑动然后再次通过while循环的东西。 任何正确方向的帮助将不胜感激。 Google尚未归还我想要的内容。

1 个答案:

答案 0 :(得分:1)

很难准确理解你想要的是什么,但是它听起来的方式,你想要滑动一次通过循环并停止直到你再次滑动。你可以做的是消除while循环,只需跟踪对象数组中的步骤。像(非常简陋,但会给你一个大致的想法):

// hold onto your position (the item currently displayed) in the module
var objIndex = 0;

// when you swipe to the next, increment the index and display the next item you fetched from the api
function moveNext() {
    objIndex += 1;
    displayData();
}
// if you can go backwards with a swipe, decrement the index and display the previous item you fetched from the db
function movePrev() {
    objIndex -= 1;
    displayData();
}

function displayData() {
    // your code to display your item on screen using the index you've saved in the module
}
相关问题