开发者

A way of disposing scripts files previously loaded with $.getScript()

Sorry about my English. I'm developing a chess interactive site. There is a lesson menu, when the user selects the lesson, it's loaded using $.getScript(). Lesson file only has a variable lesson that contains a "big" json object. When user selects another lesson, file is loaded and the new lesson var overrides the pre开发者_运维问答vious one.

Ok, that works great. But, should I dispose the previous loaded file/s, and if I should, how I do that? What if the user navigates fifty lessons, will it slow down the process?

I took a look in firebug when the file is loaded and the <head>, where supposedly $.getScript() put the file, and it makes a 'yellow flash', but nothing visible is added.

I would want to know your thoughts about this. Thanks in advance.


$.getScript doesn't actually load the file (i.e. appending it to the head as a script tag) but downloads the script .js file and runs it immediately. Of course, you could include some kind of terminating function in your script that wipes itself out when you are done using it.


Even if you'd 'dispose' the file, or delete the script tag or whatever, your script already has been exeuted, and therefore, your variables exist.

The only way would be to set to null your json objects when you select another lesson.

For example, if you have the following json in the lesson1.js file:

var lesson1data = { 
  ...Big json comes here...
};

In your main page/script you should have:

function selectLesson(lessonNumber){
   lesson1data = null; //This 'cleans' the loaded data
   lesson2data = null; //This 'cleans' the loaded data
   ...
   $.getScript(...); //Load json data for selected lesson
}

Hope this helps. Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜