dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: Execute a javascript function at a random interval

  1. #1
    Join Date
    Jul 2013
    Posts
    3

    Post Execute a javascript function at a random interval

    Hi people! I hope you can help me with this.
    I want to execute the player() function in this code every few seconds(random)

    http://pastebin.com/Kd1Hr503

    How can I make the setTimeout() function wait before it continues?

  2. #2
    Join Date
    May 2013
    Location
    Kansas City
    Posts
    21
    The setTimeout() function is really useful. But it'll only run once, unless you start nesting it. You might want to try using the setInterval() function instead, because it'll run over and over again, at least until you clear the interval out.

    I don't usually like referring people to w3schools, because the quality of the site needs to be evaluated on a per page basis with them. But, their setInterval() page is actually pretty good. http://www.w3schools.com/jsref/met_win_setinterval.asp

    Hope that helps.

  3. #3
    Join Date
    Jul 2013
    Posts
    3
    Quote Originally Posted by epricity View Post
    You might want to try using the setInterval() function instead, because it'll run over and over again, at least until you clear the interval out.
    First of all, thanks for your help! However, as I understand it, the setInterval() function runs over and over again with the SAME number of millisecs between each run. However I want to have a random amount of time which is different between each and every execution! Is that possible with setInterval() ?

  4. #4
    Join Date
    May 2013
    Location
    Kansas City
    Posts
    21
    I'm sorry man, I must have misread it.

    Would something like this work?
    http://jsfiddle.net/Akkuma/9GyyA/

  5. #5
    Join Date
    May 2013
    Location
    Kansas City
    Posts
    21
    It's a nesting solution to it, but the math looks about right.

  6. #6
    Join Date
    Jul 2013
    Posts
    3
    This looks good! thanx man.
    I will try to digest it when i am back home, since i dont understand what you did with round and curly brackets.
    And i will report back when i got it working.

  7. #7
    Join Date
    May 2013
    Location
    Kansas City
    Posts
    21
    Actually, it's not mine. I don't remember when I found it, actually.
    It's one that I bookmarked awhile back when I was trying to do something similar.
    My snippet library is nuts these days. Something for every occasion.

    Anyway, if you need any help with it, I'll be back online tonight-ish.

    Good luck, man.

    Take care.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles