2

我正在构建一个 jquery mobile + phonegap 应用程序来捆绑 iOS。JQM 站点/应用程序可以在 Web 浏览器上正常工作。但是,当与 phonegap 捆绑并在手机上进行测试时,它似乎忘记了 javascript 函数。例如,我在滑动时打开/关闭面板。几次滑动后,大约 10 次打开/关闭,它不再响应滑动。我无法打开面板。其他按钮仍然可以使用,但我无法获取面板。

在计算机或网络应用程序上,我可以整天做这件事而不会冻结。是否有可能从我的 javascript 中清除功能?或者我应该以不同的方式定义它们?

$(document).on('pageinit', '#page', function() {
  $(document).on("swipeleft swiperight", "#page", function(e) {
    console.log('swiped!!')
  });
});

有任何想法吗?

更新:

显然,当我反复尝试 10 次时,它只会“忘记”该功能。如果我在每次滑动之间留出约 2-3 秒的停顿,它似乎可以正常工作更长的时间。也许新的滑动事件正在发生,而旧的滑动事件仍在完成功能???这导致他们纠缠在一起并冻结?我一直在坚持这一点。任何有关phonegap应用程序javascript内存管理的帮助/见解都会很好。

4

1 回答 1

0

所以,我找到了解决办法。

$(document).on('pageinit', '#page', function() {
 $(document).on("swipeleft swiperight", "#page", function(e) {
   console.log('swiped!!')
 });
});

这是我发布的伪代码。事实证明,console.log 消息在每次滑动时都会被调用,但上面代码中省略的面板打开/关闭调用却没有。

这是完整的旧代码:

$(document).on('pageinit','#page', function(){
  $(document).on("swipeleft swiperight", "#page", function(e) {
    console.log('swiped!!')
    // We check if there is no open panel on the page because otherwise
    // a swipe to close the left panel would also open the right panel (and v.v.).
    // We do this by checking the data that the framework stores on the page element (panel: open).
    if ($.mobile.activePage.jqmData( "panel" ) !== "open") {
      if ( e.type === "swipeleft"  ) {
        $( "#right-panel" ).panel( "open" );
      } else if ( e.type === "swiperight" ) {
        $( "#left-panel" ).panel( "open" );
      }
    }
    else if ($.mobile.activePage.jqmData( "panel" ) == "open"){
      $( "#left-panel" ).panel( "close" );
      $( "#right-panel" ).panel( "close" );
    }
  });
}

$(document).on("swipeleft swiperight", "#page", function(e) {}这些更改修复了代码:将选择器从swipeleft swiperight 函数中移除 $(document).on("swipeleft swiperight", function(e) {} ,我添加e.stopPropagation()了事件。我认为它一定是 JQM 事件传播使 DOM 冒泡并破坏了一切。

    $(document).on('pageinit', '#page', function() {
  $(document).on("swipeleft swiperight", function(e) {
    e.stopPropagation();
    console.log('swiped!!')
    // We check if there is no open panel on the page because otherwise
    // a swipe to close the left panel would also open the right panel (and v.v.).
    // We do this by checking the data that the framework stores on the page element (panel: open).
    if ($.mobile.activePage.jqmData( "panel" ) !== "open") {
      if ( e.type === "swipeleft"  ) {
        $( "#right-panel" ).panel( "open" );
      } else if ( e.type === "swiperight" ) {
        $( "#left-panel" ).panel( "open" );
      }
    }
    else if ($.mobile.activePage.jqmData( "panel" ) == "open"){
      $( "#left-panel" ).panel( "close" );
      $( "#right-panel" ).panel( "close" );
    }
  });
    }
于 2013-06-17T16:54:22.240 回答