If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

I think that you can't stop processing in javascript, there is no sleep function. Perhaps function setTimeout is what you need (I don't know what exactly you want to achieve).
E.g.:

Code:

setTimeout(function() { alert("Hello World!"); }, 1000);

Second parameter is the number of milliseconds that the function call should be delayed by. But it doesn't stop execution of the script. During these 1000 milliseconds next lines of your code will be executed.

Thanks, Blasphemy,
You're right, setTimeout() will not do the job.
I'm using JS to position SVG hexagons on a page.
I'm looking for a sleep function to demonstrate the process.
This is the sort of code I expect to work. It doesn't.

Above code doesn't work because 'dat.getTime()' in your loop returns always the same value! Variable 'dat' constains the date and time that was exactly in the moment of calling 'var dat = new Date();' and dat.getTime() doesn't return 'current' time. Try to change

Above code doesn't work because 'dat.getTime()' in your loop returns always the same value! Variable 'dat' constains the date and time that was exactly in the moment of calling 'var dat = new Date();' and dat.getTime() doesn't return 'current' time. Try to change

Code:

do { now = dat.getTime(); }

into

Code:

do { now = Date.now(); }

Thanks Blasphemy,
I tried your suggestions, but to no avail.
Please don't be offended at my silence, I'm going away for a week.
If you're at a loose end you might want to nose into my code, html and js.
I'm not sure I can send 200 odd lines of js.
If I fail, I might try splitting it.
Here's the html.

Will suspend all program activity until e >= the time + 1 second, the variable can be re written to accomodate a functrion that when passed with the number of seconds, it will wait that amount of time, eg.

Calling 'new Date()' creates Date object containing current date and time, that is date and time that was exactly in the moment of calling this code. Method getTime() returns the number of milliseconds between midnight of January 1, 1970 and the specified date (and time). So 'n.getTime()' will return the number of milliseconds between midnight of January 1, 1970 and the date (and time) that was exactly in the moment of calling 'var n = new Date()'. It means that 'n.getTime()' in your loop condition will always return the same value, and this value will be always lower than value of 'e'.

Calling 'new Date()' creates Date object containing current date and time, that is date and time that was exactly in the moment of calling this code. Method getTime() returns the number of milliseconds between midnight of January 1, 1970 and the specified date (and time). So 'n.getTime()' will return the number of milliseconds between midnight of January 1, 1970 and the date (and time) that was exactly in the moment of calling 'var n = new Date()'. It means that 'n.getTime()' in your loop condition will always return the same value, and this value will be always lower than value of 'e'.

I haven't tested this idea, it was just a simple suggestion and your thinking is not correct, if the changes made to the function are done to correct any issues, then this is the theory.

1. call a function which will take a parameter for seconds
2. get a time reference x.getTime() for the start
3. get a future time reference which is 's' x 1000 to make the reference milliseconds and add it to a time reference of x.getTime() + future time
4. use a loop to test for this future time expiring and if it has passed, stop looping.
5. function ends.

It matters not that the time reference is based on time since 1 Jan 1970, your looking to see if that time ref + the sleep time has expired by getting a new time reference.

Any code errors are up to the person looking to use such an idea to sort out and iron out any operational problems.

So if your x.getTime() sets a variable y with a time reference of 1393716581131 and then the value of (s * 1000) is added to it and stored to y, you have a loop using a fresh date object, eg while( y<= new Date().getTime() ){}; to loop until time has passed, eg y will have a time reference of 1393716586131 if a 5 second pause is needed.

I can see one possible issue, Google Chrome... it does not play ball with some routines.

--> JavaScript Frameworks like JQuery, Angular, Node <--
... and please remember to wrap code with forum BBCode tags:- [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]If you can't think outside the box, you will be trapped forever with no escape...

Using a "while" loop for delaying the program is not a good idea. If the cumulative delays are sufficiently long the browser user will get an "unresponsive script" message, which I doubt is wanted here.

One thing you can do is to use setTimeout at the very end of the function and have it call the next function in the sequence. For example, lets say we have three functions and we want a three second delay between them.

Thanks, Tcobb,
I'm not using multiple functions.
I'm simply trying to slow down a loop.
It builds a 'honeycomb' of simple coloured hexagons.
Am I simply accumulating a bunch of setTimeouts?
Thanks for your advice.
JJ