javascript 仅在断点处执行
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
异步 XHR 调用
可能在断点存在时到达,但如果不使用断点运行,则到达该代码时 XHR 请求仍在进行中。
也许您应该对 $.ajax 调用进行回调,以在地点加载后显示下一个地点。
如果布尔值
needsToShowNextPlace
为 true,则可以让 $.ajax 处理程序调用showNextPlace
,并让showNextPlace
设置该布尔值(如果在存在时调用)没有下一个可以显示的地方。The async XHR call
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 booleanneedsToShowNextPlace
is true, and haveshowNextPlace
set that boolean if it is called when there are no next places to show.由于
$.ajax
调用是异步的,您应该将代码放入success
处理程序中,如下所示:因此您的点击绑定将是:
它不起作用,因为,您的原始代码
showNextPlace()
和photo_changed=0
在savePhoto('skip')
完成执行之前执行。 (那是异步的)。它可以使用断点,因为它给了savePhoto()
足够的时间来完成。希望这有帮助。干杯
As the
$.ajax
call is asynchronous, you should put your code in thesuccess
handler like this:So your click bind would be:
It wasn't working because, in your original code,
showNextPlace()
andphoto_changed=0
were executing beforesavePhoto('skip')
finished its execution. (thats asynchronous). It worked with breakpoint, because it gavesavePhoto()
enough time to finish.Hope this helps. Cheers
该问题与 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.