Countdown while page loads

string PreviousPageTitle = Page.Title;
int TotalIterations = 1000;
DateTime StartTime = Now;
for (int CurrentInteration = 0; CurrentInteration <= TotalIterations; CurrentInteration++) {
 if (CurrentInteration > 0) {
  int RemainingIterations = TotalIterations - CurrentInteration;
  float RemainingProgressPercent = 1 - CurrentInteration / TotalIterations;
  TimeSpan TimePassed = Now - StartTime;
  TimeSpan TimeToEnd = new TimeSpan(TotalIterations * TimePassed.Ticks / CurrentInteration);
  TimeSpan TimeRemaining = TimeToEnd - TimePassed;
  Threading.Thread.Sleep(18);
  Response.Flush();
  Response.Write(string.Format("<script>document.title = '{0} {1} {2}'</script>", RemainingIterations, TimeRemaining.ToString("m':'ss"), RemainingProgressPercent.ToString("p1")));
 }
}
Page.Title = PreviousPageTitle;
Response.Write("<script>document.title = '" + PreviousPageTitle + "'</script>");

No comments:

Post a Comment