I have a complex Web page which has many different DIVs and many differnet Javascript functions.
I want one one of these functions to Loop continually, updating the value of a certain DIV with data from the Server. But I want this Loop to Stop for X seconds before executing again. What is the best method of achieving this objective?
Just to be clear: this Stop code should not stop all Javascript code from running or freez the Web browser but should ONLY stop that given Loop for X seconds.
Do we need to call the function myfunc from setTimeout or can we just call setTimeout to stop Javascript
from running for X Seconds and then call function myfunc with parameters we want.
And what is best way to start this infinite loop, via OnLOad of the BODY?
So like this would work?
JavaScript goes at the bottom of the page just before the </body> tag - onload is almost never needed any more when you attach the JavaScript there and attaching it at the bottom allows the page to load faster and the script to run as early as possible.
setTimeout takes two parameters - the first is the function mto run and the second is the delay before running that function.
setTimeout(stay_alive, 10000);
if you need to pass parameters to the function to be called then wrap it in an anonymous function
setTimeout(function() {stay_alive(x,y)}, 10000);
As for jQuery - that is a huge amount of code to do what can otherwise be done in just a couple of lines of JavaScript - unless you need to use a lot of the JQuery functionality in your page then it is far cleaner and faster to omit it.
I agree that it’s pointless to include jQuery for things that would be trivial to implement in pure JavScript.
However the current jQuery weighs in at 91 kb in its minified form and you easily can save this amount of bytes by optimizing your site in other ways (images etc).
The reason I asked is that I’m trying to weigh up the best use case for jQuery in my own mind (it is very tempting to use it for everything) and am interested in hearing other peoples opinions.
Anyway, this is getting a little off-topic. Thanks for taking the time to address the point though.
not if all your pages average around 50k including all the images and scripts as is the case for most of my sites - also I already have my own versions of all of the various functions that JQuery provides that I actually use in my web pages toat only take up about 1k - it would be different if I were using more of the sorts of functionality that JQuery provides.
If you are going to use JQuery then a couple of things to keep in mind:
it is best to use a common copy of the JQuery library such as the one provided by Google - that way a significant fraction of your visitors will already have it cached from other sites that also reference that copy and so they will not have to download it again - that saves 91k off what they have to download to use your page compared to if you use your own copy of the script.
A reasonable level of JavaScript knowledge is required in order to properly use JQuery which you have but which many of those posting questions in the forum obviously don’t have based on some of the code I have seen (for example where someone uses 20 lines of code calling JQuery that could be done in fewer lines of JavaScript without JQuery.
From what I have seen, most JQuery questions in the forum are being asked by someone who is trying to use JQuery without knowing enough JavaScript first.
With the question being asked in this particular thread only one line of ordinary JavaScript is needed to do what was asked and so unless there is a need for JQuery elsewhere in the code there is no point including it.
1- What felgall has already explained
2- I find JQuery really ugly language
3- I think it is a waste of time to learn a language that is overlay to another language. I mean why master JQuery when mastering JavaScript is more fundamental.
Now I do not want to start a flame War about JQuery. That is 1 I am sure there are cases where JQuery is definetly a good idea, 2 for some people it makes sense to use JQuery.
Thank you for sharing your thoughts on jQuery, felgall and WorldNews.
As I said, I’m better trying to formulate my opinion on when and when not to use it, and this helps.
I especially agree with felgall’s point two. As they say, when the only tool you have is a hammer, everything starts to look like a nail
Neither of those makes sense since JQuery simnply IS JavaScript where you let someone else write a part of the JavaScript for you. Any JQuery statement is a JavaScript method call where the method called is defined using JavaScript within the JQuery oblect’s JavaScript code.
JQuery is just one JavaScript object that someone has defined for you using JavaScript. JQuery isn’t another layer on top of JavaScript it simply is JavaScript - which is why it makes no sense for people to try to use JQuery without understanding enough JavaScript to understand how JavaScript objects work.
Learning JavaScript is more fundamental than learning JQuery() or update_user() as you need JavaScript to define each of those objects before you can use them in your code.