防止在列表视图上出现更多的点击事件(一旦点击)

Prevent more tap events on a listview (once tapped)

本文关键字:事件 列表 视图      更新时间:2023-09-26

我有一个列表视图中的项目列表。点击一个li发送一个JSON请求并打开一个描述页面。

由于打开描述页需要1或2秒,因此有时间点击列表中的另一个项目,然后触发更多事件,这是我不想要的。这最终会使滚动(使用iscrollview)变得混乱,当返回列表时,底部栏会上下移动。

在处理打开描述页面时,如何停止收听列表视图上的更多点击?

没有任何可看的,我们很难帮助你。

然而,避免这种情况的最简单方法是使用全局变量作为标志。

你会设置全局变量(即:在你的JavaSCript文件的根级)为false:

tapProcessing = false;

然后,无论何时开始处理你,检查这个标志,如果不为真,则处理。

这里有一个基本的例子来告诉你我的意思:

$('.selector').click(function(e){
    if(!tapProcessing){
        //the function is not processing, therefore set the flag to true:
        tapProcessing = true;
        //do your load/etc, and reset the flag to false in the callback (when finished):
        $.get("test.php", function(data) {
            // process your data here
            // set your flag back to false:
            tapProcessing = false;
        });
    }else{
        //the function is already being processed from a previous click
    }
    e.preventDefault();  //assuming it's a link
});

下面是我添加的代码:

el是我的列表,event是登陆页将tapProcessing设置为false应该只在"changePage()请求完成将页面加载到DOM并且所有页面过渡动画都完成之后"(JQM文档:http://jquerymobile.com/demos/1.2.0/docs/api/events.html)

$('#el').click(function(e){
    if(tapProcessing) {
        alert('prevent');
        e.preventDefault();  //assuming it's a link
    } else {
        alert('oktap');
        tapProcessing = true;
    }
});
$(document).bind("pagechange", function(event, options) {
if(options['toPage']['selector'] == '#event') {
        tapProcessing = false;
    }
});

我还设置tapProcessing为false,如果我收到断开连接事件或超时。