Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript scripts make my pages take a long time to get loaded

Status
Not open for further replies.

breaststroke

Programmer
Apr 10, 2011
39
ES
Hello,

I am using several javascript functions on the pages of my website, in particular within two scripts. One of them is triggered by an event and has two fucntions within and the other has just one function.
I have realised that the pages take now quite a long time to get loaded. I know it is mainly due to these functions, but I don't know how to fix it.
For instance, I have tried adding the scripts at the botton of the file, or adding "defer" or "async" into the scripts, just before src="...", but nothing works.
I am even planning to remove one of these scripts, but I wouldn't like to do so since it is pretty important for the website.
I wonder whether there is a solution for this.
I would appreciate any help.

Thank you very much,

Regards

enjoy practicing languages at:
 
How large are your scripts?

Normally it takes a fairly large script (or a very slow machine) to noticeably slow down loading time.

So, I guess my question is, "How do you know that it is the script loading that is causing the problem?" Have you done any benchmark timing to determine exactly what is happening and when?

Have you tried putting your scripts into a standalone JS file?

When are your scripts triggered to run? During page loading? Or after the page has completely loaded?

Can the scripts be rewritten to make them faster to load and faster to execute? Is there any bloat that can be removed?

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
Hello again,

thank you mmerlinn for your reply.
how many questions!:)
i think my pages are not that slow now. I think my computer has been slow these days, Now it takes less time for my pages to get loaded.
But in despite of that I think they are getting loaded in more time than usuall.
I even tried removing my javascript scripts and it took them much less time.
I don't think I can improve my scripts to get the down loading time to be faster. I managed to include two functions onto one page, but the other one has a timeout, and I don't know why but if I include it with the others it doesn't work.

I, also have to put the scripts into the head element, due to the same reason.

None of the scripts are very long actually, but the one with the timeOut is running on its own and connecting to the database. I have coded it the simplest way possible though.
I think this script may be a reason for the long downloading time, although I am not sure.

i have also tried adding "async" or "defer" as I said above but it doesn't seem to work.

thank you.

enjoy practicing languages at:
 
Sounds to me that your timeout script is the culprit.

I don't understand why you can't combine the scripts. That sounds fishy to me as if there is some coding error in your timeout script. You might want to debug your timeout script since it looks like that is where your problem is.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
hi again,

this is the script with the timeOut, which seems to be slowing the pages down.
It works fine. the only proble may be that one.
I am trying to find out what is wrong with it. I put it up here, just in case you can see anything wrong mmerlinn (or someone else;D):

<script type="text/javascript">
var t;
var timer_is_on=0;
function chatear()
{
var xmlhttp;
if(window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
if(xmlhttp.responseText!="")
{
alert(xmlhttp.responseText+" "+"is trying to chat with you.");
}
}
}
xmlhttp.open("GET","somechat.php",true);
xmlhttp.send();
setInterval("chatear()", 4000);
}
if(!timer_is_on)
{
timer_is_on=1;
chatear();
}
</script>

May be "somechat.php" slowing down the page as well?
Thank you

enjoy practicing languages at:
 
Hi again,

I don't think this thread is followed anymore, because it's quite old xd, but I wanted to post now because I think i have found the problem.
As you suggested mmerlinn, and I suspected too, somehow, the problem was within the script with the timeOut.
I have changed the following:

setInterval("chatear()", 4000);

to:

t=setTimeout("chatear()",4000);


At least for now the pages are running quite smoothly.
And indeed, I have a page with a lot of javascript code and it was quite fast to load. It had that part of the script similar to my new change.

In any case, if someone comes up with a better way to write that part of the code, I would really appreciate it, lest this one stops working fine at any point.;P

Thnaks

enjoy practicing languages at:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top