javascript 仅在断点处执行

发布于 2024-11-18 05:23:09 字数 1207 浏览 1 评论 0原文

我对 JS 和 jQuery 有一个非常奇怪的问题。

$('#skip').click(function(e) {
   savePhoto('skip');
   showNextPlace();
   photo_changed = 0;
   return false;
});

showNextPlace()photo_changed = 0; 仅在我添加断点时执行。 savePhoto(); 的代码非常基本。我根据位置数组中的状态和元素数量提交请求。

function savePhoto( status )
  {
      if ( places.length <= 5 )
      {
          // load more places
          $('#loadmore').val('1');
      }

      // Don't send a request on skip unless we need more places
      if ( status != 'skip' || $('#loadmore').val() == 1 )
      {
          $.ajax({
              url: "url" + status,
              cache: false,
              data: $('#form_photo').serialize(),
              success: function(results){
                  // dont load more places until we have few
                  $('#loadmore').val('0');

                  if ( results != '[]' )
                  {
                      for ( var i in results )
                      {
                          places.push( results[i] );
                      }
                  }
              }
          });
      }
  }

知道为什么会发生这种情况或者如何更好地调试问题吗?

I have a really weird problem with JS and jQuery.

$('#skip').click(function(e) {
   savePhoto('skip');
   showNextPlace();
   photo_changed = 0;
   return false;
});

showNextPlace() and photo_changed = 0; are only executed when I add a breakpoint. The code of savePhoto(); is very basic. I submit a request depending on the status and the number of elements in the places array.

function savePhoto( status )
  {
      if ( places.length <= 5 )
      {
          // load more places
          $('#loadmore').val('1');
      }

      // Don't send a request on skip unless we need more places
      if ( status != 'skip' || $('#loadmore').val() == 1 )
      {
          $.ajax({
              url: "url" + status,
              cache: false,
              data: $('#form_photo').serialize(),
              success: function(results){
                  // dont load more places until we have few
                  $('#loadmore').val('0');

                  if ( results != '[]' )
                  {
                      for ( var i in results )
                      {
                          places.push( results[i] );
                      }
                  }
              }
          });
      }
  }

Any idea on why this happens or how can I debug better the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄司 2024-11-25 05:23:09

异步 XHR 调用

$.ajax({ ... })

可能在断点存在时到达,但如果不使用断点运行,则到达该代码时 XHR 请求仍在进行中。

也许您应该对 $.ajax 调用进行回调,以在地点加载后显示下一个地点。

如果布尔值 needsToShowNextPlace 为 true,则可以让 $.ajax 处理程序调用 showNextPlace,并让 showNextPlace 设置该布尔值(如果在存在时调用)没有下一个可以显示的地方。

The async XHR call

$.ajax({ ... })

is probably arriving while the breakpoint is there, but when not run with a breakpoint, the XHR request is still in-flight when that code is reached.

Maybe you should have a callback for the $.ajax call that shows the next place after the places have loaded.

You could have the $.ajax handler call showNextPlace if a boolean needsToShowNextPlace is true, and have showNextPlace set that boolean if it is called when there are no next places to show.

握住你手 2024-11-25 05:23:09

由于 $.ajax 调用是异步的,您应该将代码放入 success 处理程序中,如下所示:

success: function(results){
  // dont load more places until we have few
  $('#loadmore').val('0');
  if ( results != '[]' )
  {
      for ( var i in results )
      {
          places.push( results[i] );
      }
  }
   showNextPlace(); //Moved here
   photo_changed = 0; //Moved here  
}

因此您的点击绑定将是:

$('#skip').click(function(e) {
   savePhoto('skip');
   return false;
});

它不起作用,因为,您的原始代码 showNextPlace()photo_changed=0savePhoto('skip') 完成执行之前执行。 (那是异步的)。它可以使用断点,因为它给了 savePhoto() 足够的时间来完成。

希望这有帮助。干杯

As the $.ajax call is asynchronous, you should put your code in the success handler like this:

success: function(results){
  // dont load more places until we have few
  $('#loadmore').val('0');
  if ( results != '[]' )
  {
      for ( var i in results )
      {
          places.push( results[i] );
      }
  }
   showNextPlace(); //Moved here
   photo_changed = 0; //Moved here  
}

So your click bind would be:

$('#skip').click(function(e) {
   savePhoto('skip');
   return false;
});

It wasn't working because, in your original code, showNextPlace() and photo_changed=0 were executing before savePhoto('skip') finished its execution. (thats asynchronous). It worked with breakpoint, because it gave savePhoto() enough time to finish.

Hope this helps. Cheers

不羁少年 2024-11-25 05:23:09

该问题与 AJAX 调用无关。在代码的另一部分中,每次用户按下保存按钮时,我都会将一个事件绑定到图像字段(因此第二次执行代码 2 次,第三次执行 3 次,等等。)请记住取消绑定!

此外,作物的某些部分和部分即使我单击“跳过”,保存插件也会被执行,因此我添加了一些控制变量。

解决方案:在逻辑可以走的每条路径上使用console.log()。

The problem wasn't related to the AJAX call. In another part of the code, I was binding an event to the image field each time the user pressed the save button (so the second time the code was being executes 2 times, the 3rd 3, etc..) Remember to unbind!

Also, some part of the crop & save plugin was being executed even if I clicked on skip, so I have added some control variables.

Solution: console.log() on every path that the logic can go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文