时间-秒表
using System;using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using Model;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["userID"] != null)
{
int uid = Convert.ToInt32(Request.Cookies["userID"].Value.ToString().Trim());
ViewState["UserID"] = uid;
DateTime dt = DateTime.Now;
int year = dt.Year;
int month = dt.Month;
int day = dt.Day;
DateTime time = new DateTime(year, month, day, 0, 0, 0);
int i = time.ToString().IndexOf(" ");
Time.Text = time.ToString().Substring(i);
}
else
{
Timer1.Dispose();
}
}
}
/// <summary>
/// 秒表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Timer1_Tick(object sender, EventArgs e)
{
int userID = Convert.ToInt32(ViewState["UserID"]);
string str = Time.Text;
if (str != "")
{
string[] obj = str.Split(':');
//0:00:00
int hour = Convert.ToInt32(obj[0]);
int minute = Convert.ToInt32(obj[1]);
int second = Convert.ToInt32(obj[2]);
if (second < 60)
second++;
if (second == 60)
{
minute++;
if (minute != 0 && minute % 1 == 0)
{
//30分钟增加10个积分
DateTime time = DateTime.Now;
UserEPManager.UserExpByOnLine(Convert.ToInt32(ViewState["UserID"]), time,10);
}
second = 0;
}
if (minute == 60)
{
hour++;
minute = 0;
}
string h = hour.ToString();
string m = minute.ToString();
string s = second.ToString();
if (Convert.ToInt32(m) < 10)
{
m = "0" + m;
}
if (Convert.ToInt32(s) < 10)
{
s = "0" + s;
}
Time.Text = h + ":" + m + ":" + s;
}
}
}
秒表在本机测试可以正常,,但是通过局域网,用另一台电脑访问这个页面,秒表变慢了,慢了两秒钟
就是本机页面的秒表正常 走2秒,通过局域网另一台电脑打开相同的页面上秒表 才走1秒
这是什么问题呢,谢谢各位gg、jj --------------------编程问答-------------------- 用 Timer 处发一次算一秒。是不会准确的。Timer1_Tick中 你应该计算两个 datetime 的间隔。才是真正页面停留时间。用ViewState["StartTime"] 记录第一次时间。每次Timer1_Tick中用 datetime.now,和ViewState["StartTime"] 记算间隔 --------------------编程问答-------------------- 可以用javascript实现:
<script type="text/javascript">
function showTime()
{
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
h = check(h);
s = check(s);
document.getElementById("mydiv").innerHTML = "Now,time is " + h + ":" + m + ":" + s;
t = setTimeout("showTime()",500);
}
function check(n)
{
if( n < 10 )
{
n = "0" + n;
}
return n;
}
</script>
--------------------编程问答--------------------
用这个 应该能实现 --------------------编程问答-------------------- 1 不要用服务器端来用秒表,浏览器请求,服务器响应本来就要时间,然后解析、反馈时间肯定不准确,而且你更新得靠浏览器来刷新。
2 建议使用js在客户端自己使用秒表
--------------------编程问答--------------------
代码不能用 --------------------编程问答--------------------
帮顶
补充:.NET技术 , C#