内存管理在javascript jquery手机与phonegap

Memory management in javascript jquery mobile with phonegap

本文关键字:手机 phonegap jquery javascript 管理 内存      更新时间:2023-09-26

我正在构建一个jquery手机+ 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内存管理的帮助/见解将是很好的。

所以,我找到了一个解决方案。

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

这是我发布的伪代码。结果是,每次滑动时都会调用console.log msg,但是上面代码中省略的面板打开/关闭调用却没有。

完整的旧代码:

$(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" );
    }
  });
}

这些修改修复了以下代码:将选择器从swipelleft swiperight功能中移除$(document).on("swipeleft swiperight", "#page", function(e) {}变成了$(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" );
    }
  });
    }