Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

sleep function

fh791fh791 Posts: 35Member
Hi there,
I need a sleep function like this:

function sleep(sleepDuration){
}

to pause the program execution for sleepDuration milliseconds.
I'm asking if someone can help.
Thanks,
«1

Comments

  • aaronjones3593aaronjones3593 Posts: 122Member
    : Hi there,
    : I need a sleep function like this:
    :
    : function sleep(sleepDuration){
    : }
    :
    : to pause the program execution for sleepDuration milliseconds.
    : I'm asking if someone can help.
    : Thanks,
    :

    [code]
    //your code before "sleep"
    [red]window.setTimeout("function_1()",sleepDuration_ms);[/red]
    function_1()
    {
    //your code after "sleep"
    }
    [/code]

    Use that approach all the time ;)

    -( Aaron )-
  • NCS_OneNCS_One Posts: 170Member
    : : Hi there,
    : : I need a sleep function like this:
    : :
    : : function sleep(sleepDuration){
    : : }
    : :
    : : to pause the program execution for sleepDuration milliseconds.
    : : I'm asking if someone can help.
    : : Thanks,
    : :
    :
    : [code]
    : //your code before "sleep"
    : [red]window.setTimeout("function_1()",sleepDuration_ms);[/red]
    : function_1()
    : {
    : //your code after "sleep"
    : }
    : [/code]
    :
    : Use that approach all the time ;)
    :
    : -( Aaron )-
    :

    Hi

    I think thats not what he wants.
    setTimeout - Evaluates an expression after a specified number of milliseconds has elapsed.
    If sleepDuration_ms = 10000 then function_1() will be called every 10 seconds.
  • fh791fh791 Posts: 35Member
    Dear Aaron,

    Thanks for your help. But I want to use the sleep function in a for loop like this:

    for(...) {
    statement 1
    statement 2
    sleep(1000) //////
    statement 3;
    }

    The way you told can not be used here.
  • BodkinBodkin Posts: 100Member
    The setTimeout and setInterval are different from a sleep function in that they create threads (programs that run in parallel with other programs). Right after the thread is created the program continues executing the next line. eg:
    [CODE][SIZE=2]
    function testSetTimeout() {
    setTimeout("someFunc()", 1000);
    for(a in someArray);
    }

    function someFunc() {
    alert("Time has run out");
    }
    [/SIZE][/CODE]
    So right after setTimeout is executed, and not 1000 millisec after, the FOR statement is executed.
    The only direct delay you can make is a FOR statement like for(a=0;a<1e6;a++), but it will hold the browser prisoner until is has finished. If the script runs for too long you can experience the browser asking you if you want to stop the script.

    I would suggest that you rethink your design and maybe consider using Java instead if you really really need that sleep function.
  • aaronjones3593aaronjones3593 Posts: 122Member
    : Dear Aaron,
    :
    : Thanks for your help. But I want to use the sleep function in a for loop like this:
    :
    : for(...) {
    : statement 1
    : statement 2
    : sleep(1000) //////
    : statement 3;
    : }
    :
    : The way you told can not be used here.
    :
    I don't know then :S


    Sorry,

    -( Aaron )-
  • aaronjones3593aaronjones3593 Posts: 122Member
    [b][red]This message was edited by aaronjones3593 at 2005-12-26 6:48:54[/red][/b][hr]
    [b][red]This message was edited by aaronjones3593 at 2005-12-26 6:48:7[/red][/b][hr]
    : : : Hi there,
    : : : I need a sleep function like this:
    : : :
    : : : function sleep(sleepDuration){
    : : : }
    : : :
    : : : to pause the program execution for sleepDuration milliseconds.
    : : : I'm asking if someone can help.
    : : : Thanks,
    : : :
    : :
    : : [code]
    : : //your code before "sleep"
    : : [red]window.setTimeout("function_1()",sleepDuration_ms);[/red]
    : : function_1()
    : : {
    : : //your code after "sleep"
    : : }
    : : [/code]
    : :
    : : Use that approach all the time ;)
    : :
    : : -( Aaron )-
    : :
    :
    : Hi
    :
    : I think thats not what he wants.
    : setTimeout - Evaluates an expression after a specified number of milliseconds has elapsed.
    : If sleepDuration_ms = 10000 then function_1() will be called every 10 seconds.
    :


    Odd.. It's never done it every duration seconds for me, I use that if I want to pause something and it only calls the function once..

    unless I've gone mad, well, I never said I was sane did I?


    -( Aaron )-




    Edit: Oh, I give up trying to make duration italics.. :(
  • aaronjones3593aaronjones3593 Posts: 122Member
    : Hi there,
    : I need a sleep function like this:
    :
    : function sleep(sleepDuration){
    : }
    :
    : to pause the program execution for sleepDuration milliseconds.
    : I'm asking if someone can help.
    : Thanks,
    :

    I have had time to think about it, and I think I have come up with a solution for you:

    [code]
    for ([red]i = 1; i < 10; i++[/red])
    {
    timeOut = window.setTimeout("function_01(i)",i*sleepDuration);
    }
    function function_01(i)
    {
    window.clearTimeout(timeOut);
    timeOut = null;
    [red]alert(i);[/red]
    }

    var timeOut = null;
    [/code]

    Change the bits in red according to what code you want to be processed in your for.. loop

    Hope that helps,

    -( Aaron )-

    Peace all...

  • BodkinBodkin Posts: 100Member
    [b][red]This message was edited by Bodkin at 2005-12-28 10:59:34[/red][/b][hr]
    Dear aaronjones3593!

    Although your code will work, there's a few remarks to be made:

    1) The function passed to [italic]setTimeout[/italic] is not evaluated until the timeout actually occurs. So you cannot use the variable [italic]i[/italic] in the way that you do, as it will always equal the last value assigned to [italic]i[/italic] (10 in this case). Use [italic]"function_01("+i+")"[/italic] instead.

    2) [italic]setTimeout[/italic] will not perform in the way you think. Program execution continues right after [italic]setTimeout[/italic] has been called. There's is NO delay. So the timeouts will come long after the for-loop has finished, which is not what [b]fh791[/b] wants.

    3) An easier and simpler way of achieving what your code does is to use [italic]window.setInterval[/italic], like this:

    [CODE][SIZE=2]

    function init(duration, repeat) {
    interval = window.setInterval("outOfTime()", duration);
    count = repeat;
    }
    function outOfTime() {
    alert("Oops I did it again!
    count = "+count);
    if(--count == 0)
    window.clearInterval(interval);
    }



    [/SIZE][/CODE]

    4) As I read the original message by [b]fh791[/b] his aim was not to have more than one sleep period (Albeit several times as he wants it inside a for-loop). (Yes he can set that with your code, but then the whole point of your code setting up several timeouts is lost)

    5) Referring to my first reply http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=35&MsgID=325035&Setting=A9999F0001: The whole issue of "sleeping" should be thought through carefully, it can adversely effect you browser performance.

    [SIZE=3][b]I'd much rather have fh791 to specify why he needs to take a "nap"; is it some kind of animation?[/b][/SIZE]


    PS. There's a link on the bottom of the reply-page ([blue]Click here for instructions[/blue] pops up a window) that shows you which tags you can use.
    [leftbr]b[rightbr]
    [leftbr]italic[rightbr]
    [leftbr]leftbr[rightbr]
    [leftbr]rightbr[rightbr]


  • NCS_OneNCS_One Posts: 170Member
    [b][red]This message was edited by NCS_One at 2005-12-28 14:39:55[/red][/b][hr]
    : [b][red]This message was edited by Bodkin at 2005-12-28 10:59:34[/red][/b][hr]
    : Dear aaronjones3593!
    :
    : Although your code will work, there's a few remarks to be made:
    :
    : 1) The function passed to [italic]setTimeout[/italic] is not evaluated until the timeout actually occurs. So you cannot use the variable [italic]i[/italic] in the way that you do, as it will always equal the last value assigned to [italic]i[/italic] (10 in this case). Use [italic]"function_01("+i+")"[/italic] instead.
    :
    : 2) [italic]setTimeout[/italic] will not perform in the way you think. Program execution continues right after [italic]setTimeout[/italic] has been called. There's is NO delay. So the timeouts will come long after the for-loop has finished, which is not what [b]fh791[/b] wants.
    :
    : 3) An easier and simpler way of achieving what your code does is to use [italic]window.setInterval[/italic], like this:
    :
    : [CODE][SIZE=2]
    :
    : function init(duration, repeat) {
    : interval = window.setInterval("outOfTime()", duration);
    : count = repeat;
    : }
    : function outOfTime() {
    : alert("Oops I did it again!
    count = "+count);
    : if(--count == 0)
    : window.clearInterval(interval);
    : }
    :
    :
    :
    : [/SIZE][/CODE]
    :
    : 4) As I read the original message by [b]fh791[/b] his aim was not to have more than one sleep period (Albeit several times as he wants it inside a for-loop). (Yes he can set that with your code, but then the whole point of your code setting up several timeouts is lost)
    :
    : 5) Referring to my first reply http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=35&MsgID=325035&Setting=A9999F0001: The whole issue of "sleeping" should be thought through carefully, it can adversely effect you browser performance.
    :
    : [SIZE=3][b]I'd much rather have fh791 to specify why he needs to take a "nap"; is it some kind of animation?[/b][/SIZE]
    :
    :
    : PS. There's a link on the bottom of the reply-page ([blue]Click here for instructions[/blue] pops up a window) that shows you which tags you can use.
    : [leftbr]b[rightbr]
    : [leftbr]italic[rightbr]
    : [leftbr]leftbr[rightbr]
    : [leftbr]rightbr[rightbr]
    :
    :
    :

    Hi

    I just tryed your code and it doesnt work, the program still runs.

    I was doing it with the getTime, i get the current time then i
    add the sleepseconds to the current time into a var (ex : Num1)then i enter in a do while, and in there im always getting the current time and comparing it with var Num1 and if current time bigger then var Num1
    then it exits the do while, but dont know why im getting an error in the do while maybe because it call to many times the getTime, dont know.


  • aaronjones3593aaronjones3593 Posts: 122Member
    : [b][red]This message was edited by Bodkin at 2005-12-28 10:59:34[/red][/b][hr]
    : Dear aaronjones3593!
    :
    : Although your code will work, there's a few remarks to be made:
    :
    : 1) The function passed to [italic]setTimeout[/italic] is not evaluated until the timeout actually occurs. So you cannot use the variable [italic]i[/italic] in the way that you do, as it will always equal the last value assigned to [italic]i[/italic] (10 in this case). Use [italic]"function_01("+i+")"[/italic] instead.
    :
    : 2) [italic]setTimeout[/italic] will not perform in the way you think. Program execution continues right after [italic]setTimeout[/italic] has been called. There's is NO delay. So the timeouts will come long after the for-loop has finished, which is not what [b]fh791[/b] wants.
    :
    : 3) An easier and simpler way of achieving what your code does is to use [italic]window.setInterval[/italic], like this:
    :
    : [CODE][SIZE=2]
    :
    : function init(duration, repeat) {
    : interval = window.setInterval("outOfTime()", duration);
    : count = repeat;
    : }
    : function outOfTime() {
    : alert("Oops I did it again!
    count = "+count);
    : if(--count == 0)
    : window.clearInterval(interval);
    : }
    :
    :
    :
    : [/SIZE][/CODE]
    :
    : 4) As I read the original message by [b]fh791[/b] his aim was not to have more than one sleep period (Albeit several times as he wants it inside a for-loop). (Yes he can set that with your code, but then the whole point of your code setting up several timeouts is lost)
    :
    : 5) Referring to my first reply http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=35&MsgID=325035&Setting=A9999F0001: The whole issue of "sleeping" should be thought through carefully, it can adversely effect you browser performance.
    :
    : [SIZE=3][b]I'd much rather have fh791 to specify why he needs to take a "nap"; is it some kind of animation?[/b][/SIZE]
    :
    :
    : PS. There's a link on the bottom of the reply-page ([blue]Click here for instructions[/blue] pops up a window) that shows you which tags you can use.
    : [leftbr]b[rightbr]
    : [leftbr]italic[rightbr]
    : [leftbr]leftbr[rightbr]
    : [leftbr]rightbr[rightbr]
    :
    :
    :


    Hmm, you do have several points there.

    Thanks for the heads-up on the bbcode too :)

    Judging from the reply by NCS_One, it looks like your code does not work either though...

    Maybe there just is no way to put something to sleep :(

    -( Aaron )-

    Peace all...

«1
Sign In or Register to comment.