【问题标题】:Memory management in javascript jquery mobile with phonegap带有phonegap的javascript jquery mobile中的内存管理
【发布时间】:2013-06-10 21:01:06
【问题描述】:

我正在构建一个 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 的内存管理的帮助/见解都会很好。

【问题讨论】:

  • 如果您删除 pageinit 事件,我想它会正常工作。试试看。
  • 这更糟了。它根本不起作用,bc jqm 还没有加载。但即使我将它包裹在 $(function() {}) 中,它的行为也类似——在几次滑动后工作,然后停止/忘记。它很奇怪

标签: javascript jquery-mobile memory-management cordova


【解决方案1】:

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

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

这些更改修复了代码: 将选择器从 swipeleft 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" );
    }
  });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 2015-06-08
    • 2015-09-03
    • 2014-07-13
    相关资源
    最近更新 更多