More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Sunday, 21 May 2017 8:14:49 AM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 23/11/2008(UTC)
Posts: 520

Javascript sleep, setTimeout and setInterval

Javascript doesn't have sleep() or wait() function. setTimeout(expression, timeout) runs the function once after the timeout. It behaves like creating another thread for the scheduled task. setInterval(expression, timeout); executes the function in intervals, with the length of the timeout between them. So setTimeout and setInterval are not a sleep or wait function.

Example:

Code:
var intervalId = setInterval(function(){}, 100); // will execute the function every 100ms.
// clearInterval(intervalID); // Will clear the timer.

setTimeout(function(){}, 100); // will execute the function once, after 100ms.

    <script type="text/javascript">
      /**
       * Delay for a number of milliseconds
       */
      function sleep(delay) {
        var start = new Date().getTime();
        while (new Date().getTime() < start + delay);
      }
    </script>
Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.