简体   繁体   English

一段时间后页面无响应

[英]Page becomes unresponsive after some time

I have a problem with my jQuery/Javascript code. 我的jQuery / JavaScript代码有问题。 All works as expected but after some time the page becomes unresponsive throwing the Unresponsive Script error for jQuery. 一切正常,但是一段时间后页面变得无响应,并抛出jQuery Unresponsive Script错误。 I am using a jQuery Library for displaying lists as coverflow. 我正在使用jQuery库将列表显示为Coverflow。

Also, after all calls to backend I have to refresh the particular div and hence I destroy and recreate the div every time the call is made to backend. 另外,在所有对后端的调用之后,我必须刷新特定的div,因此每次调用后端时都会销毁并重新创建div。

Please let me know what is the problem in the structure of the code given below: 请让我知道以下给出的代码结构中的问题:

(Please note this is just a snippet with important methods. All these methods also have other logic written in them which is not displayed here). (请注意,这只是重要方法的摘要。所有这些方法也都编写了其他逻辑,此处未显示)。

 function showAllData(dataFromServer) { $('#child').remove(); $('#parent').append($('<div id="child"></div>')); $.each(dataFromServer.someArray, function(index, item) { var html = '<li>' + item + '</li>'; $('#child').append($(html)); }); //Attach Event to div $("#child").on("click", function() { removeTag(); }); }; $(document).ready(function() { //got data from server(Spring MVC) in a var 'dataFromServer' //code not written here showAllData(dataFromServer); $("#child").flipster(); //a coverflow library }); $(document).on('submit', '#formSubmit', function(event) { event.preventDefault(); $.ajax({ url: url, contentType: 'application/json; charset=utf-8', dataType: 'json', async: true, cache: false, data: JSON.stringify({ dataFromClient: dataFromServer }), type: "POST", success: function(data) { dataFromServer = data; showAllData(dataFromServer); $("#child").flipster(); }, error: function(xhr, status, error) { console.log(xhr.responseText); } }); return false; }); function removeTag() { $.ajax({ url: 'deleteBooks', contentType: 'application/json; charset=utf-8', dataType: 'json', async: true, cache: false, data: JSON.stringify({ keyword: tag, dataFromClient: dataFromServer }), type: "POST", success: function(data) { dataFromServer = data; showAllData(dataFromServer); $("#child").flipster(); }, error: function(xhr, status, error) { console.log(xhr.responseText); } }); }; 

Any idea? 任何想法?

[EDIT]: This happens quickly when accessing website on mobile. [编辑]:在手机上访问网站时,这种情况很快发生。 The page freezes! 页面冻结! But when working on desktop version, page becomes unresponsive after some time and error of Maximum Call Stack Size Reached error is thrown. 但是当使用桌面版本时,页面在一段时间后变得无响应,并引发“ Maximum Call Stack Size Reached错误。

This can be because of the memory of smartphone. 这可能是由于智能手机的内存。 Nonetheless, problem is there in both versions. 尽管如此,两个版本都存在问题。

You have some recursion going on which is exponentially adding #child .click() handlers. 您正在进行一些递归操作,它以指数方式添加了#child .click()处理函数。 I have made this jsFiddle to simplify your code and demo what's going on. 我做了这个jsFiddle来简化您的代码并演示正在发生的事情。 Open the console, click 'submit' to start, then click a few times on one of the list items. 打开控制台,单击“提交”开始,然后在列表项之一上单击几次。 You'll see for each click, the console outputs more and more lines. 您将看到每次单击,控制台将输出越来越多的行。

Try moving $("#child").on("click"...){} outside of function showAllData(){} 尝试将$("#child").on("click"...){}function showAllData(){}

Update : So that the handler works after you remove and re-add #child you can add the click listener like this: 更新 :为了使处理程序在删除并重新添加#child之后可以工作,您可以像这样添加点击侦听器:

$('#parent').on('click', '#child', function() {
    // 
});

I've put this in an updated jsFiddle . 我把它放在更新的jsFiddle中

Update 2 : I saw that Flipster was not re-initialising after you refresh #child and came up with this: 更新2 :刷新#child并提出以下建议后,我发现Flipster并未重新初始化:

Change $("#child").flipster(); 更改$("#child").flipster(); to $("#parent").flipster(); $("#parent").flipster(); . Then after doing $('#child').remove() in showAllData() : 然后在showAllData()执行$('#child').remove()之后:

// remove the flipster classes from #parent or flipster will not re-initialise when called next time.
$('#parent').removeClass('flipster').removeClass(function(index, css) {
    return (css.match(/(^|\s)flipster-\S+/g) || []).join(' ');
});

I put this in a codePen . 我把它放在codePen中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM