ASP.NET website: đồng hồ đếm ngược

ASP.NET website: đồng hồ đếm ngược

Bạn nhận một đơn đặt hàng thiết kế website yêu cầu có một đồng hồ thời gian đếm ngược (VD: đấu giá, thời gian khuyến mãi, v.v...) nhưng làm thế nào chèn một bộ đếm thời gian vào website và mỗi khi thời gian nhích một giây vẫn không làm cho trang web bị load lại. WEBXAULA.COM xin trình bày một cách đơn giản để có thể thực hiện được điều này. 

 ASP.NET website: đồng hồ đếm ngược

 Tại trang Default.aspx, bạn chèn code sau vào phần source HTML:

 

<div id="timelabel"></div>

 
<script type="text/javascript">
var leave =<%=seconds %>;
CounterTimer();
var interv=setInterval(CounterTimer,1000);
function CounterTimer()
{
var day = Math.floor(leave / ( 60 * 60 * 24))
var hour = Math.floor(leave / 3600) - (day * 24)
var minute = Math.floor(leave / 60) - (day * 24 *60) - (hour * 60)
var second = Math.floor(leave) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60)
hour=hour<10 ? "0" + hour : hour;
minute=minute<10 ? "0" + minute : minute;
second=second<10 ? "0" + second : second;
var remain=day + " days   "+hour + ":"+minute+":"+second;
leave=leave-1;
document.getElementById("timelabel").innerHTML=remain;
}
</script>
 
Sau đó bạn chèn code sau vào trang Default.aspx.cs:
 
public double seconds;
    protected void Page_Load(object sender, EventArgs e)
    {
        seconds = (GetEndTime() - GetStartTime()).TotalSeconds;
    }
    private DateTime GetStartTime()
    {
        return DateTime.Now;
    }
    private DateTime GetEndTime()
    {
        return new DateTime(2010, 5, 06, 8, 10, 0);
    }
 

Như vậy là OK rồi. Chúc các bạn thành công.

Bạn thấy bài viết này như thế nào?: 
Average: 9 (3 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
How To Get Facebook For Android Phones

How To Get Facebook For Android Phones

It may look like a very simple task; browsing through the Android market to get an app that suits you, but the reality is its not.

Manage Photos between Facebook, Google+, Flickr with Primadesk Android App

Manage Photos between Facebook, Google+, Flickr with Primadesk Android App

Some months back we mention about a web service Primadesk ,which allows you to copy and manage photos between Facebook, Flickr

6 cách tăng tốc độ tải cho website của bạn

6 cách tăng tốc độ tải cho website của bạn

Bạn đang điều hành và phát triển một website cá nhân hay thương mại điện tử, hiển nhiên, điều quan trọng nhất là bạn phải thực hiện seo website.

Công ty diệt chuột T&C

 

Diet con trung