在获取ajax回调时,可能导致Chrome崩溃的原因是什么?(What could cause Chrome to crash when getting an ajax callback?)

编程入门 行业动态 更新时间:2024-10-25 06:29:25
在获取ajax回调时,可能导致Chrome崩溃的原因是什么?(What could cause Chrome to crash when getting an ajax callback?)

我的javascript(技术类型脚本)代码中有以下几行:

debug.print("send ajax call : "+file); jQuery.get('img/' + file + '.txt', this.ProcessFileData.bind(this)); // 'bind' should force the callback to remember its object - part of ECMAScript 5 debug.print("ajax call sent : " + file);

(调试功能是我自己的,它只是写入窗口中的文本框,但它允许我看到Chrome到目前为止没有崩溃。)但是在Chrome中从未达到ProcessFileData()开头的调试行(版本27.0.1453.110),即使它在IE,Firefox和Safari中工作正常。 Chrome没有出现错误窗口,但是标签变得没有响应 - 我无法启动开发人员工具,或者选择页面上的文本等(虽然我可以切换到其他标签)。 更重要的是,在ProcessFileData函数中存在一些差异的代码的先前版本,但显然在调试消息的位置之后,在Chrome中也可以正常工作。

用以下任一项替换中间行:

jQuery.get('img/' + file + '.txt', this.ProcessFileData); jQuery.get('img/' + file + '.txt', function () { debug.print("check"); });

阻止Chrome崩溃(虽然这意味着程序的其余部分停止按预期工作),所以它似乎必然是导致问题的“绑定”方法 - 即使这已经成为我项目的一部分6个月(我不能没有它就不能让回调正常工作)这个问题只在几个星期前出现了。 我正在使用ddr-ecma5库与旧版浏览器兼容,但我尝试将其取出并没有任何效果。

有人知道回调中的bind方法是如何导致崩溃的,以及我如何解决这个问题? 有什么方法可以避免使用bind方法(解决问题)并仍然让ProcessFileData函数知道正在返回哪个文件?

I have the following lines in my javascript (technically typescript) code:

debug.print("send ajax call : "+file); jQuery.get('img/' + file + '.txt', this.ProcessFileData.bind(this)); // 'bind' should force the callback to remember its object - part of ECMAScript 5 debug.print("ajax call sent : " + file);

(The debug function is my own, it just writes to a textbox in the window, but it allows me to see that Chrome isn't crashing so far.) But a debug line at the beginning of ProcessFileData() never gets reached in Chrome (Version 27.0.1453.110), even though it works fine in IE, Firefox and Safari. Chrome doesn't come up with an error window, but the tab becomes unresponsive - I can't bring up developer tools, or select the text on the page etc. (though I can switch to other tabs). What's more, a previous version of the code with some differences in the ProcessFileData function, but obviously after where the debug message would have been, works OK in Chrome too.

Replacing the middle line with either of the following:

jQuery.get('img/' + file + '.txt', this.ProcessFileData); jQuery.get('img/' + file + '.txt', function () { debug.print("check"); });

stops Chrome from crashing (although it means the rest of the program stops working as intended), so it seems that it must be the 'bind' method causing a problem - even though that has been part of my project for 6 months (I couldn't get the callbacks to work correctly without it) and this issue only cropped up a few weeks ago. I'm using the ddr-ecma5 library for compatibility with older browsers, but I've tried taking that out with no effect.

Does anybody know how the bind method in a callback causes a crash, and how I might get around this? Is there any way I can avoid using the bind method (which solves the problem) and still let the ProcessFileData function know which file is being returned?

最满意答案

我完全不知道为什么你的调用会失败 - 听起来你的代码中有一个无限循环。

要避免使用bind ,请将其外部保存在其他变量中,并在匿名函数中调用ProcessFileData :

var that = this; jQuery.get('img/' + file + '.txt', function(e) { that.ProcessFileData(e); });

I have absolutely no idea why your call should fail -- it sounds like you have an infinite loop somewhere in your code.

To avoid using bind, save the outer this in some other variable and invoke ProcessFileData in an anonymous function:

var that = this; jQuery.get('img/' + file + '.txt', function(e) { that.ProcessFileData(e); });

更多推荐

本文发布于:2023-08-04 05:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409349.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   原因   ajax   Chrome   callback

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!