My application has developed using django framework and jQuery. After run the application for a while it always not responding or "Aw,Snap!" in Chrome within unexpected time.
The issue that I thought first is running javascript every 2 seconds with loading some values. I have a few pages that run this kind of scripts. The scripts are like this:
$(document).ready(function(){
$(window).load(function loadMap(){
var mappos = $("#mappos").val();
var pcode = $("#pcode").val();
var width = $("#width").val();
var loss = $("#loss").val();
var lossarr = $("#lossarr").val();
var spcode = $("#spcode").val();
var swidth = $("#swidth").val();
var cpcode = $('#minclamp', top.document).contents().find('#cpcode').val();
var cwidth = $('#minclamp', top.document).contents().find('#cwidth').val();
var lane = $('#minclamp', top.document).contents().find('#lane').val();
var position = $('#minclamp', top.document).contents().find('#position').val();
var atlane = $('#minclamp', top.document).contents().find('#atlane').val();
var atposition = $('#minclamp', top.document).contents().find('#atposition').val();
var clamping = $("#clamping").val();
var changed = $("#changed").val();
var realtag = $('#minclamp', top.document).contents().find('#realtag').val()
$("#inventory").load(inventory_url+"?pcode="+pcode+"&width="+width+"&loss="+loss+"&lossarr="+lossarr+"&spcode="+spcode+"&swidth="+swidth+"&cpcode="+cpcode+"&cwidth="+cwidth+"&lane="+lane+"&position="+p开发者_开发知识库osition+"&atlane="+atlane+"&atposition="+atposition+"&clamping="+clamping+"&changed="+changed+"&realtag="+realtag+"&mappos="+mappos);
}
setTimeout(loadMap, 2000);
});
});
If I am right, is there any other codes that do the same but not lead to not responding event like this. Or is there any technique to improve this?
If there should be other issues to think about please suggest me. Thank you.
Try to put the settimeout in the loadMap function... after all the work is finished... and use the $(document).ready function
$(document).ready(function ()
{
var loadMap = function()
{
var mappos = $("#mappos").val();
var pcode = $("#pcode").val();
var width = $("#width").val();
var loss = $("#loss").val();
var lossarr = $("#lossarr").val();
var spcode = $("#spcode").val();
var swidth = $("#swidth").val();
var cpcode = $('#minclamp', top.document).contents().find('#cpcode').val();
var cwidth = $('#minclamp', top.document).contents().find('#cwidth').val();
var lane = $('#minclamp', top.document).contents().find('#lane').val();
var position = $('#minclamp', top.document).contents().find('#position').val();
var atlane = $('#minclamp', top.document).contents().find('#atlane').val();
var atposition = $('#minclamp', top.document).contents().find('#atposition').val();
var clamping = $("#clamping").val();
var changed = $("#changed").val();
var realtag = $('#minclamp', top.document).contents().find('#realtag').val()
$("#inventory").load(inventory_url+"?pcode="+pcode+"&width="+width+"&loss="+loss+"&lossarr="+lossarr+"&spcode="+spcode+"&swidth="+swidth+"&cpcode="+cpcode+"&cwidth="+cwidth+"&lane="+lane+"&position="+position+"&atlane="+atlane+"&atposition="+atposition+"&clamping="+clamping+"&changed="+changed+"&realtag="+realtag+"&mappos="+mappos, function()
{
// set the timer here in the completeCallback of your 'load' function.
setTimeout(function()
{
loadMap();
}, 2000);
}));
};
loadMap();// call the first load map;
});
Have you tried using separate threads?
EDIT: if you know what you are looking for: http://www.websiteoptimization.com/speed/10/ Very nice
精彩评论