js定时器、高亮修改单元格背景色
window.setInterval()
功能:按照指定的周期(以毫秒計)來調(diào)用函數(shù)或計算表達式。
語法:setInterval(code,millisec)
?
解釋:code:在定時時間到時要執(zhí)行的JavaScript代碼串,js函數(shù)
millisec:設(shè)定的定時時間,用毫秒數(shù)表示。
返回值:定時器的ID值,可用于clearInterval()方法停止指定的定時器。
注:setInterval()方法會不停地調(diào)用函數(shù),直到用clearInterval()終止定時或窗口被關(guān)閉。
window.clearInterval()
功能:取消由setInterval()方法設(shè)置的定時器。
語法:clearInterval(id_of_setinterval)
解釋:id_of_setinterval:由setInterval()返回的ID值。該值標識了一個setInterval定時器。
也就是:window.setInterval()返回的就是window.clearInterval的參數(shù)
例子:
<script type="text/javascript"> var count = 0; var timeID; function timeCount() { document.getElementByIdx('timetxt').value = count; count++; } function beginCount() { timeID = setInterval("timeCount()",1000); } function stopCount() { clearInterval(timeID); } </script> <input type="button" value="開始計時" οnclick="beginCount()" /> <input type="text" id="timetxt" size="5" /> <input type="button" value="停止計時" οnclick="stopCount()" /> 再如: var objTimer = window.setInterval("moveDiv()",10)是調(diào)動定時器,其中moveDiv是js的一個函數(shù)if(objTimer) window.clearInterval(objTimer)是停止定時器
?
2、單元格高亮變色
<script type="text/javascript">
????? var k=0;
????? function? highlightTableRows(tableId){
????? k=k+1;
?????? var table = document.getElementById(tableId);??
?????? var tbody = table.getElementsByTagName("tbody")[0];?
?????? if (tbody == null){?
??????? var rows = table.getElementsByTagName("tr");?
??????? } else {?
??????? var rows = tbody.getElementsByTagName("tr");?
??????? }
??????? for(var i=0;i<rows.length;i++){
????????? var tds=rows[i].getElementsByTagName("td");
????????? var tdMax=0;
????????? for(var j=1;j<tds.length;j++){
??????????? var strs=tds[j].innerHTML;
??????????? var array=strs.split("/");
??????????? var str=array[1];
??????????? if(str>=1.8){
????????????? tds[j].style.backgroundColor="red";?
??????????? }else if(str<1.8&&str>=1.35){
????????????? tds[j].style.backgroundColor="yellow";
????????????? console.log("yellow");
??????????? }else if(str<1.35&&str>=1){
????????????? //tds[j].style.backgroundColor="yello";
??????????? }else if(str<1){
???????????? tds[j].style.backgroundColor="green";
??????????? }
??????????? if(str>tdMax){
?????????????? tdMax=str;
??????????? }
????????? }
????????? if(tdMax>=1.8){
????????????? tds[0].style.backgroundColor="red";?
??????????? }else if(tdMax<1.8&&tdMax>=1.35){
????????????? tds[0].style.backgroundColor="yellow";
??????????? }else if(tdMax<1.35&&tdMax>=1){
????????????? //tds[0].style.backgroundColor="yello";
??????????? }else if(tdMax<1){
???????????? tds[0].style.backgroundColor="green";
??????????? }?
??????? }?
??????? if(k>15){
???????????? window.clearInterval(timer);
??????? }???
???? }
??? var timer = window.setInterval("highlightTableRows('app')", 1000);
?</script>
?
部分引自:http://www.cnblogs.com/liences/archive/2011/11/25/2262883.html
轉(zhuǎn)載于:https://www.cnblogs.com/Defry/p/4588357.html
總結(jié)
以上是生活随笔為你收集整理的js定时器、高亮修改单元格背景色的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android调用蓝牙打印机
- 下一篇: 使用AngularJS 进行Hybri