❶ 用js实现计时器功能
脚空租坦本说明:
第一步:把如下代码加入<head>区域中
<SCRIPT language=javascript>
<!--
function generate(form){for(var q=0;q<12;q++){if(document.me.m.options[document.me.m.selectedIndex].value==q){var m2=q+1
}var txt='<!-- 分三步完成全部脚本:\r\n\r\n'
+' 1. 将第一部分粘贴到HTML的HEAD区\r\n'
+' 2. 将OnLoad事件加入BODY标签内\r\n'
+' 3. 将最后一部分代码加入BODY区 -->\r\n\r\n'
+'<!-- 第一步: 将如下代码粘贴到HTML的HEAD区斗桐-->\r\n\r\n'
+'<HEAD>\r\n\r\n<SCRIPT LANGUAGE="JavaScript">\r\n\r\n'
+'<!-- Begin\r\n'
+'var Temp2;\n'
+'var timerID = null;\n'
+'var timerRunning = false;\n'
+'function arry() {\n'
+'this.length = 12;\n'
+'this[0] = 31;\n'
+'this[1] = 28;\n'
+'this[2] = 31;\n'
+'this[3] = 30;\n'
+'this[4] = 31;\n'
+'this[5] = 30;\n'
+'this[6] = 31;\n'
+'this[7] = 31;\n'
+'this[8] = 30;\n'
+'this[9] = 31;\n'
+'this[10] = 30;\n'
+'this[11] = 31;\n'
+'}\n'
+'var date = new arry();\n'
+'\n'
+'function stopclock() {\n'
+'if(timerRunning)\n'
+'clearTimeout(timerID);\n'
+'timerRunning = false;\n'
+'}\n'
+'\n'型饥
+'function startclock() {\n'
+'stopclock();\n'
+'showtime();\n'
+'}\n'
+'\n'
+'function showtime() {\n'
+'now = new Date();\n'
+'var CurMonth = now.getMonth();\n'
+'var CurDate = now.getDate();\n'
+'var CurYear = now.getFullYear();\n'
+'now = null;\n'
+'if ('+document.me.d.options[document.me.d.selectedIndex].value+' < CurDate) {\n'
+'CurDate -= date[CurMonth]; CurMonth++;\n'
+'}\n'
+'if ('+document.me.m.options[document.me.m.selectedIndex].value+' < CurMonth) {\n'
+'CurMonth -= 12; CurYear++;\n'
+'}\n'
+'\n'
+'var Yearleft = '+document.me.y.options[document.me.y.selectedIndex].value+' - CurYear;\n'
+'var Monthleft = '+document.me.m.options[document.me.m.selectedIndex].value+' - CurMonth;\n'
+'var Dateleft = '+document.me.d.options[document.me.d.selectedIndex].value+' - CurDate;\n'
+'\n'
+'document.dateform.a.value = Yearleft;\n'
+'document.dateform.b.value = Monthleft;\n'
+'document.dateform.c.value = Dateleft;\n'
+'\n'
+'timerID = setTimeout("showtime()",1000);\n'
+'timerRunning = true;\n'
+'}\n'
+'/\/ End -->\r\n<\/script>\r\n'
+'<\/H'+'EAD>\r\n\r\n'
+'<!-- 第二步:将OnLoad事件加入BODY标签内 -->\r\n\r\n'
+'<BO'+'DY Onload="startclock()">\r\n\r\n'
+'<!-- 第三步:将最后一部分代码加入BODY区 -->\r\n\r\n'
+'<form name=dateform>距离'
+m2+'/'+document.me.d.options[document.me.d.selectedIndex].value+'/'
+document.me.y.options[document.me.y.selectedIndex].value
+'还有\n'
+'<input type=text name=a size=2 value="">年\n'
+'<input type=text name=b size=2 value="">月\n'
+'<input type=text name=c size=2 value="">天\n'
+'</fo'+'rm>\r\n\r\n'
+'<'+'!-- 代码长度: 1.95 KB --'+'>'
;}document.mail.source.value=txt;document.mail.source2.value=txt;}
//-->
</SCRIPT>
第二步:把如下代码加入区域中:
<center>
<table border=5 bordercolor=blue borderlight=green>
<tr><td align=center><font size=5 color=red face="Arial, Helvetica, sans-serif"><strong>下面框中为脚本显示区</strong></font></td></tr>
<tr><td align=center><form name=me> <p><select name=m size=1> <option value=0>January </option> <option value=1>February </option> <option value=2>March </option> <option value=3>April </option> <option value=4>May </option> <option value=5>June </option> <option value=6>July </option> <option value=7>August </option> <option value=8>September </option> <option value=9>October </option> <option value=10>November </option> <option value=11>December </option> </select> <select name=d size=1> <option value=1>1 </option> <option value=2>2 </option> <option value=3>3 </option> <option value=4>4 </option> <option value=5>5 </option> <option value=6>6 </option> <option value=7>7 </option> <option value=8>8 </option> <option value=9>9 </option> <option value=10>10 </option> <option value=11>11 </option> <option value=12>12 </option> <option value=13>13 </option> <option value=14>14 </option> <option value=15>15 </option> <option value=16>16 </option> <option value=17>17 </option> <option value=18>18 </option> <option value=19>19 </option> <option value=20>20 </option> <option value=21>21 </option> <option value=22>22 </option> <option value=23>23 </option> <option value=24>24 </option> <option value=25>25 </option> <option value=26>26 </option> <option value=27>27 </option> <option value=28>28 </option> <option value=29>29 </option> <option value=30>30 </option> <option value=31>31 </option> </select><select name=y size=1> <option value=1999>1999 </option> <option value=2000>2000 </option> <option value=2001>2001 </option> <option value=2002>2002 </option> <option value=2003>2003 </option> <option value=2004>2004 </option> <option value=2005>2005 </option> <option value=2006>2006 </option> <option value=2007>2007 </option> <option value=2008>2008 </option> <option value=2009>2009 </option> </select><br> <input type=button onclick=generate() value=生成代码 class=yk9><input type=button value=返回 onClick=history.go(-1) name=button class=yk9><p></p></form><form name=mail> <input type=hidden name=scriptname value="Countdown Creator"><input type=hidden name=source2 value><table bgcolor=dedfdf border=1 cellpadding=1 width=396> <tr> <td align=center height=218><textarea name=source rows=12 cols=55 class=yk9></textarea><br> <br> </td> <td> </td> </tr> </table></form></td></tr></table>
❷ JS倒计时页面代码(要求读取服务器时间)
用JavaScript获取服务器时间,然后做页面倒计时的程序代码如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"lang="en">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>untitled</title>
<scripttype="text/javascript">
get=function(id){
returndocument.getElementById(id)
}
if(document.all){
window.XMLHttpRequest=function(){
varget=['Microsoft.XMLHTTP','Msxml2.XMLHTTP'];
for(vari=0;i<get.length;i++)
{
try{
returnnewActiveXObject(get[i])
}
catch(e){}
};
};
}
webDate=function(fn){
varHtime=newXMLHttpRequest();
Htime.onreadystatechange=function(){
Htime.readyState==4&&(fn(newDate(Htime.getResponseHeader('Date'))))
};
Htime.open('HEAD','/?_='+(-newDate));Htime.send(null);
}
window.time=newDate();
targetTime=newDate();
time2String=function(t){
with(t)return[getFullYear(),'年',('0'+(getMonth()+1)).slice(-2),'月',('0'+getDate()).slice(-2),'日',('0'+getHours()).slice(-2),':',('0'+getMinutes()).slice(-2),':',('0'+getSeconds()).slice(-2)].join('')
}
int2time=function(m){
m-=(D=parseInt(m/86400000))*86400000;m-=(H=parseInt(m/3600000))*3600000;S=parseInt((m-=(M=parseInt(m/60000))*60000)/1000);
returnD+'天'+H+'小时'+M+'分'+S+'秒'
}
setInterval(function(){
webDate(function(webTime){
get('web').innerHTML=time2String(time=webTime);
})
get('locale').innerHTML=time2String(newDate);
get('time').innerHTML=int2time(targetTime-time);
if((targetTime-time)<0){
get('time').innerHTML='GameOver';
}},1000)
</script>
</head>
<body>
设定时间:2015年06月18日0时0分0秒<br>服务器时间:<spanid='web'>loading...</span><br>
本地时间:<spanid="locale">loading...</span><br>
倒计时时间:<spanid="time">loading...</span>
<scripttype="text/javascript"charset="utf-8">targetTime=newDate(2015,06,18,00,00,00);</script>
</body>
</html>
注:原理用xmlhttp来获取服务器上的时间,后台用js做倒计时显示到页面上,由于获取服务器时间这个在本地运行不一定能成功,最好是在服务器上运行!
❸ html如何用JS自动弹出一个DIV
也就先做一个DIV。里面的内容都做好。
此DIV背后加一个mask层。
把这个DIv默认设置为隐藏。
JS里加一个定时器,每隔一分钟,设置为这个DIv的css样式中的display=block
❹ 怎样用JS实现数据的批量提交
在一个项目中用Excel组件OWC.Spreadsheet在客户端链友导出HTML格式的表格为Excel文件,对于数据量较大的表格导出会造成IE窗口迟唤链停止响应,用户体验很差。想到三个解决方案: 1、用applet结合jxl实现。暂不考虑。 2、利用类似Ajax请求的机制,异步调用码孙一个导出方法,该方法在导出完毕后回调一个callback方法。 3、利用类似Java中的线程机制,直接开一个导出线程即可。
❺ 如何实现JS 计时器
//html
<div>当前时间为:<spanid="timeNow"></span></div><br>
<buttonid="timeBegin">计时开始</button>
<buttonid="timeEnd">计时结束</button>
<buttonid="timeClear">计时清除</button>
//Javascript
<scripttype="text/javascript">
//定义初始值计时器
varcount=0;
vartimer="";
//开始计时
functionBeginTime(){
varbeginBtn=document.getElementById("timeBegin");
beginBtn.onclick=function(){
timer=setInterval(function(){
count++;
document.getElementById("timeNow").innerHTML=count/100;
},10)
}
}
//结束计时
functionEndTime(){
varendBtn=document.getElementById("timeEnd");
endBtn.onclick=function(){
clearInterval(timer);
}
}
//计时清除
functionClearTime(){
varclearBtn=document.getElementById("timeClear");
clearBtn.onclick=function(){
document.getElementById("timeNow").innerHTML="";
count=0;
}
}
BeginTime();
EndTime();
ClearTime();
</script>
思路: 很简单
一个盒子来显示时间, 三个按钮控制时间 开始 、 结束、 归零
定义一个初始值和计时器,点开始计时器初始值不断增加,点结束停止计时器,点清除把时间盒子清空,初始值归零即可
记录生活的瞬间,分享学习的心得,感悟生活,留住感动,( www.jensonhui.com )
❻ JS时间倒计时并到时自动提交表单,做个考试页面,登录后两个小时答题,要在页面显示开始和结束时间
<!DOCTYPEhtml>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>倒计时</title>
<style>
diva{float:left;margin-left:18px;}
.jrotate{
width:80px;
padding:0px;
}
.jrotate>div{
width:36px;
height:36px;
overflow:hidden;
padding:0px;
margin:0px;
margin-right:2px;
float:right;
}
.jrotatedivdiv{
text-align:center;
background:#CCC;
width:36px;
height:36px;
margin:0px;
font-size:36px;
}
.time{
font-size:24px;
}
</style>
<scriptsrc="jq-2.1.0.js"></script>
</head>
<body>
<divstyle="text-align:center;">
<divclass="not-start"style="display:none;">
<h1>考试还未开始</h1>
</div>
<divclass="show-jishi"style="display:none;">
<h1>考试结束倒计时</h1>
</div>
<divclass="show-end"style="display:none;">
<h1>考试已经结束</h1>
</div>
<h1time></h1>
<divstyle="padding-left:36%;">
<a>
<divday>
</div>
<divclass="time">
days
</div>
</a>
<a>
<divhour>
</div>
<divclass="time">
hours
</div>
</a>
<a>
<divminute>
</div>
<divclass="time">
minutes
</div>
</a>
<a>
<divsecond>
</div>
<divclass="time">
seconds
</div>
</a>
<divstyle="clear:both"></div>
</div>
<formaction="#"class="show-jishi">
<div>考试内容在这</div>
</form>
</div>
<script>
functionJrotate(cfg){
cfg=$.extend({},Jrotate.defaults,cfg);
this.init(cfg);
}
$.fn.Jrotate=function(){
return歼吵newJrotate({renderTo:this[0]});//注意这里返回的不是JQ对象
}
Jrotate.prototype={
init:function(cfg){
this.cfg=cfg;
弊清if(!cfg.renderTo)return;
cfg.renderTo=$(cfg.renderTo);
if(cfg.renderTo[0].isJrotate)return;
cfg.renderTo[0].isJrotate=true;
if(cfg.class){
cfg.renderTo.addClass(cfg.class);
}
氏卜侍cfg.showNumber=cfg.showNumber||0;
cfg.showNumber+='';
this.makeDefaultList(Math.max(cfg.startLength,cfg.showNumber.length),cfg.startNumber);
this.setNumber(cfg.showNumber);
},
makeDefaultList:function(len,num){
if(len<1)return;
varcfg=this.cfg;
num=num||0;
num+='';
while(num.length<len){
num='0'+num;
}
varcfg=this.cfg;
for(vari=0;i<len;i++){
if(cfg.renderTo.children('div').length>=cfg.maxLength)return;
cfg.renderTo.prepend(this.makeList(num.charAt(i)));
}
},
setNumber:function(num){
if(num==this.lastNumber)return;
if(isNaN(num))return;
varcfg=this.cfg;
num+='';
varlength=num.length;
varlen=cfg.renderTo.children('div').length;
this.makeDefaultList(Math.max(cfg.startLength,length)-len);
length=Math.max(length,len,cfg.startLength);
while(num.length<length){
num='0'+num;
}
var_this=this;
cfg.renderTo.children('div').each(function(index){
index=length-index-1;
varchar=num.charAt(index);
if(this.showIndex==char)return;
varobj={ele:this,index:index};
setTimeout(function(){_this.gotoIndex(obj.ele,char,1)},5);
});
this.lastNumber=num;
},
gotoIndex:function(ele,index,sts){
ele=$(ele);
varh=ele.children('div:first').height();
varcfg=this.cfg;
ele[0].showIndex=index;
ele.animate({
scrollTop:index*h
},sts&&cfg.speed);
},
makeList:function(num){
num=num||0;
var_this=this;
varul=$("<div>");
for(vari=0;i<10;i++){
varli=$('<div>');
ul.append(li.html(i));
}
setTimeout(function(){_this.gotoIndex(ul,num,0)},2);
returnul;
}
}
Jrotate.defaults={
renderTo:'#div',//显示效果的对象
startNumber:0,
showNumber:0,
maxLength:2,
startLength:2,
class:'jrotate',//样式
speed:500//动画速度
};
varlastTime='2014-05-2412:00:00';//截止时间
varstartTime='2014-05-2316:00:00';//开始时间
vardays=$('[day]').Jrotate();
varhours=$('[hour]').Jrotate();
varminutes=$('[minute]').Jrotate();
varseconds=$('[second]').Jrotate();
vartimes={
day:days,
hour:hours,
minute:minutes,
second:seconds
}
varshowTime;
functionsetTime(){
vardate=newDate().getTime();
if(date<startTimes){
$('.not-start').show();
showTime=startTimes;
}elseif(date>lastTimes){
alert('考试结束');
$('form')[0].submit();
}else{
$('.not-start').hide();
$('.show-jishi').show();
showTime=lastTimes;
}
var_time={}
vart=(showTime-date)/1000|0;//总的秒数
_time.day=t/(60*60*24)|0;//天数
t-=_time.day*60*60*24;
_time.hour=t/(60*60)|0;//小时数
t-=_time.hour*60*60;
_time.minute=t/60|0;//分钟
_time.second=t-_time.minute*60;//秒数
$('[time]').html(_time.day+'天'+_time.hour+'小时'+_time.minute+'分'+_time.second+'秒');
for(variintimes){
times[i].setNumber(_time[i]);
}
}
vardate=newDate().getTime();
varstartTimes=newDate(startTime).getTime();
varlastTimes=newDate(lastTime).getTime();
if(date>lastTimes){
$('.show-end').show().nextAll().remove();
}else{
setTime();
setInterval(setTime,500);
}
</script>
</body>
</html>
请修改具体的开始时间 和截止时间来展示不同效果 包括未开始 正在进行和已经完毕的处理
❼ js中计时器返回数值
JS可以实现很多java代码不易完成的功能。这里学习一些js中的计时器事件。
JavaScript 一个设定的时间间隔之后来执行代码,称之为计时事件。
主要通过两个方法来实现:
1.setInterval() - 间隔指定的毫秒数不停地执行指定的代码。
2.setTimeout() - 暂停指定的毫秒数后执行指定的代码
并且,这两个方法都是window对象的方法。
首先,介绍setInterval()方法,该方法值得是间隔一定的毫秒数不停的执行指定的代码。
语法:window.setInterval(”js代码,函数等“,毫秒数);
实例1:每三秒弹出一个hello
setInterval(function(){alert("Hello")},3000);
实例2:显示当前时间,通过按钮实现时间的停止,开始
<script type="text/javascript">
var myVar;
function startTimer(){
/*setInterval() 间隔指定的毫秒数不停地执行指定的代码*/
myVar=setInterval(function(){myTimer()},1000);
}
function myTimer()/* 定义一个得到本地时间的函数*/
{
var d=new Date();
var t=d.toLocaleTimeString();
document.getElementById("demo").innerHTML=t;
}
function stopTimer()
{/* clearInterval() 方法用于停止 setInterval() 方法执行的函数代码*/
clearInterval(myVar);
}
</script>
setTimeout() 方法
在html或jsp中
<body>
<h4 id="demo"></h4>
<input type="button" onclick="startTimer()" value="开始">
<input type="button" onclick="stopTimer()" value="停止">
</body>
对于setTimeout()方法,指的是指定的毫秒数后执行指定的代码或方法。
语法:window.setTimeout("javascript 函数",毫秒数);
实例1:3秒钟后弹出”hello“提示框
setTimeout(function(){alert("Hello")},3000);
实例2:三秒钟后跳转到前一个页面
<script type="text/javascript">
setTimeout(function(){
window.history.back();
},3000);
</script>
如何执行停止呢?
clearTimeout() 方法用于停止执行setTimeout()方法的函数代码。这里注意myVar必须是一个全局变量。实例如下:
var myVar;
function myFunction()
{
myVar=setTimeout(function(){alert("Hello")},3000);
}
function myStopFunction()
{
clearTimeout(myVar);
}
以上就把js计时器的基本功能简要介绍了,具体的使用场景,想要深刻理解,只有到项目用到时方可有新的理会。
❽ jquery或js前端提交数据的几种方式
1.jquery提交数据的方式:
(1)第一种jquery序列化提交数据方式:
通过id获取的form表单元素.serialize();
(2)第二种模拟form表单提交元素:
$('#form表单id').attr('method','post');
$('#form表单id'').find('input[name="type"]').val(test);
$('#form表单id').find('input[name="dfrom_to1"]').val(dfrom_to);
$('#form表单id').find('input[name="gt_road_new"]').val(gt_road);
$('#form表id').attr('action',AdminLTE.ctx+'/moles/ltegt/findAllCoverAndInterfere.do');
$('#analysisForm').submit();
2.js提交数据的方式:
(1).js提交表单( .submit()方法提交表单 )
function doSearch(){
var action ="<%=path%>/User_queryAllUser";
document.all.form.action = action;
document.all.form.submit();}
(2).js替代超链接( window.location.href )
<input type="button" id="modify" value="修改工号" οnclick="modifyEmp(${ myList.employeeId })">
//js不能起名为modify,为敏感关键字
function modifyEmp( employeeId ){
//employeeId 作为js的参数传递进来
window.location.href = '<%=path%>/User_openUserUpdate?employeeId='+employeeId;
❾ 用JS实现钟表计时器功能
利用new Date();可以轻松的实现钟表功能,甚至日历功能.
如果要实现计时器功能也可以用这个对象.
var c = 1000; // 一千微秒,就是一秒
function funBeginDisTime() {
c = c + 1000; // 节奏为一秒
var now = new Date(0,0,0,0,0,0,c);
var day = now.getDate();
var hour = now.getHours();
var minutes = now.getMinutes();
var sec = now.getSeconds();
$("#myClock").html(day + "天"+ hour + "时" + minutes + "分" + sec + "秒");
myTime = setTimeout("funBeginDisTime()", 1000); // 每一秒执行一次
}
function funStopDisTime() {
clearTimeout(myTime);
}
body>
<input id="Button2" type="button" value="开始" onclick="funBeginDisTime()"/>
<span id="myClock"></span>
<input id="Button1" type="button" value="暂停" onclick="funStopDisTime()" />
</body>
setInterval() 是循环重复执行某个动作,
setTimeout()是只执行一次.
比如每五秒就通过AJAX向服务器发送一次请求.那么就可以用setInterval():
[javascript] view plain
setInterval("reloadAction()", 5000);
[javascript] view plain
function reloadAction() {
$.ajax({
"type":"POST",
"url":"live.php",
"data":"getData=live",
"success":function(data) {
// ....
}
});
}
❿ JS 倒计时实现代码(时、分,秒)
JS实现倒计时(时、分,秒)
var
interval
=
1000;
function
ShowCountDown(year,month,day,divname)
{
var
now
=
new
Date();
var
endDate
=
new
Date(year,
month-1,
day);
var
leftTime=endDate.getTime()-now.getTime();
var
leftsecond
=
parseInt(leftTime/1000);
//var
day1=parseInt(leftsecond/(24*60*60*6));
var
day1=Math.floor(leftsecond/(60*60*24));
var
hour=Math.floor((leftsecond-day1*24*60*60)/3600);
var
minute=Math.floor((leftsecond-day1*24*60*60-hour*3600)/60);
var
second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60);
var
cc
=
document.getElementById(divname);
cc.innerHTML
=
"脚本之家提示距离"+year+"年"+month+"月"+day+"日还有:"+day1+"天"+hour+"小时"+minute+"分"+second+"秒";
}
window.setInterval(function(){ShowCountDown(2010,4,20,'divdown1');},
interval);
[Ctrl+A
全选
注:如需引入外部Js需刷新才能执行]