I have a problem with my jQuery/Javascript code. All works as expected but after some time the page becomes unresponsive throwing the Unresponsive Script
error for jQuery.
I am using a jQuery Library for displaying lists as 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.
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.
This can be because of the memory of smartphone. Nonetheless, problem is there in both versions.