scriptmanager调用webservice实现的倒计时

最近要做一个招聘系统,其中涉及到了笔试的倒计时功能,找了一些解决方法,在这里记录一种是用scriptmanager调用webservice的实现方法,倒计时间的计算是服务端计算的,即使客户端刷新页面或重新点击开始倒计时也不管用。
aspx文件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JsWebService倒计时.aspx.cs"
Inherits="AJAXEnabledWebApplication1.JsWebService倒计时" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JsWebService倒计时</title>
<script type="text/javascript" src="killF5.js"></script>

<script type="text/javascript">
var totalSeconds; //剩余时间(秒)
var timer; //倒计时控制器
var o_blink; //闪烁
var isstart = false; //标记是否开始

//开始倒计时
function kaishi() {
//取消闪烁
if ($get("msg").innerHTML != "") {
$get("msg").innerHTML = "";
stopblink();
}
AJAXEnabledWebApplication1.CountdownService.GetTotalSeconds(onSucceeded); //获取剩余时间(秒)
//开始倒计时
timer = setInterval("startCountdown()", 1000);
isstart = true;
}
//结束倒计时
function jieshu() {
clearInterval(timer);
isstart = false;
}
//倒计时函数
function startCountdown() {
AJAXEnabledWebApplication1.CountdownService.GetTotalSeconds(onSucceeded); //获取剩余时间(秒)
if (totalSeconds > 0) {
var days = Math.floor(totalSeconds / 86400);
var hours = Math.floor(totalSeconds % 86400 / 3600);
var minutes = Math.floor(totalSeconds % 3600 / 60);
var seconds = Math.floor(totalSeconds % 60);

$get("lblDays").innerHTML = days;
$get("lblHours").innerHTML = (hours >= 10 ? hours : "0" + hours);
$get("lblMinutes").innerHTML = (minutes >= 10 ? minutes : "0" + minutes);
$get("lblSeconds").innerHTML = (seconds >= 10 ? seconds : "0" + seconds);
}
else {
clearInterval(timer);
$get("lblSeconds").innerHTML = "00";
$get("msg").innerHTML = "答题时间到!";
blink("msg");
isstart = false;
}
}
//成功获取剩余时间后的回调函数
function onSucceeded(result) {
totalSeconds = result;
}

//时间到提示闪烁
function blink(elId) {
var html = '';
if (document.all)
html += 'var el = document.all.' + elId + ';';
else if (document.getElementById)
html += 'var el = document.getElementById("' + elId + '");';
html +=
'el.style.visibility =' +
'el.style.visibility == "hidden" ? "visible" : "hidden"';
if (document.all || document.getElementById)
o_blink = setInterval(html, 500)
}
//停止闪烁
function stopblink() {
clearInterval(o_blink);
}

</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="CountdownService.asmx" />
</Services>
</asp:ScriptManager>
<div id="DivCountdown" style="border-style: solid;" align="center">
JsWebService倒计时<br />
<br />
<input id="Button1" type="button" value="开始计时" onclick="kaishi()" />
 &nbsp
<input id="Button2" type="button" value="结束计时" onclick="jieshu()" />
<br />
<br />
<asp:Label ID="lblDays" runat="server" Text="0" ForeColor="Red" />天
<asp:Label ID="lblHours" runat="server" Text="0" ForeColor="Red" />时
<asp:Label ID="lblMinutes" runat="server" Text="0" ForeColor="Red" />分
<asp:Label ID="lblSeconds" runat="server" Text="0" ForeColor="Red" />秒
<br />
<span id="msg" style="height: 20px; background-color: #ffe4c4;"></span>
</div>
</div>
</form>
</body>
</html>

webservice.cs文件,考试时长可以在web.config文件中定义或者将对应试题的考试时间保存到数据库,然后从数据库中提取。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
//
using System.Configuration;

namespace AJAXEnabledWebApplication1
{
/// <summary>
/// CountdownService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class CountdownService : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public int GetTotalSeconds()
{
DateTime StartTime = DateTime.Now; //开始时间
if (Session["StartTime"] != null)
{
StartTime = (DateTime)Session["StartTime"];
}
else
{
Session["StartTime"] = StartTime;
}
DateTime EndTime = StartTime.Add(TimeSpan.FromHours(double.Parse(ConfigurationManager.AppSettings["timeLength"].ToString())));
DateTime NowTime = DateTime.Now;
TimeSpan Countdown = EndTime - NowTime;
if (Countdown.TotalSeconds<=0)
{
Session["StartTime"] = null;
}
return (Countdown.TotalSeconds > 0) ? (int)Countdown.TotalSeconds : 0;
}
}
}

另外还添加了屏蔽f5和右键的js,一起附上,防止客户端刷新页面。

//屏蔽f5
function DisableF5() {
with (event) {
// F5 and Ctrl+R
if (keyCode == 116 || (ctrlKey && keyCode == 82)) {
event.keyCode = 0;
event.cancelBubble = true;
return false;
}
}
}
document.onkeydown = DisableF5;

//屏蔽网页右鍵,适用于IE6,IE7,IE8,FireFox,谷歌Chrome浏览器
function clickIE4() {
if (event.button == 2) {
return false;
} //end if
} //end func

function clickNS4(e) {
if (document.layers || document.getElementById && !document.all) {
if (e.which == 2 || e.which == 3) {
return false;
} //end if
} //end if
} //end func

function OnDeny() {
if (event.ctrlKey || event.keyCode == 78 && event.ctrlKey || event.altKey || event.altKey && event.keyCode == 115) {
return false;
} //end if
}

if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown = clickNS4;
document.onkeydown = OnDeny();
} else if (document.all && !document.getElementById) {
document.onmousedown = clickIE4;
document.onkeydown = OnDeny();
} //end if

document.oncontextmenu = new Function("return false");
资源下载链接为: https://pan.quark.cn/s/5c50e6120579 在Android移动应用开发中,定位功能扮演着极为关键的角色,尤其是在提供导航、本地搜索等服务时,它能够帮助应用获取用户的位置信息。以“baiduGPS.rar”为例,这是一个基于百度地图API实现定位功能的示例项目,旨在展示如何在Android应用中集成百度地图的GPS定位服务。以下是对该技术的详细阐述。 百度地图API简介 百度地图API是由百度提供的一系列开放接口,开发者可以利用这些接口将百度地图的功能集成到自己的应用中,涵盖地图展示、定位、路径规划等多个方面。借助它,开发者能够开发出满足不同业务需求的定制化地图应用。 Android定位方式 Android系统支持多种定位方式,包括GPS(全球定位系统)和网络定位(通过Wi-Fi及移动网络)。开发者可以根据应用的具体需求选择合适的定位方法。在本示例中,主要采用GPS实现高精度定位。 权限声明 在Android应用中使用定位功能前,必须在Manifest.xml文件中声明相关权限。例如,添加<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />,以获取用户的精确位置信息。 百度地图SDK初始化 集成百度地图API时,需要在应用启动时初始化地图SDK。通常在Application类或Activity的onCreate()方法中调用BMapManager.init(),并设置回调监听器以处理初始化结果。 MapView的创建 在布局文件中添加MapView组件,它是地图显示的基础。通过设置其属性(如mapType、zoomLevel等),可以控制地图的显示效果。 定位服务的管理 使用百度地图API的LocationClient类来管理定位服务
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值