zoukankan      html  css  js  c++  java
  • js定时器、高亮修改单元格背景色

    window.setInterval()

    功能:按照指定的周期(以毫秒计)来调用函数或计算表达式。

    语法:setInterval(code,millisec)

    解释:code:在定时时间到时要执行的JavaScript代码串,js函数

    millisec:设定的定时时间,用毫秒数表示。

    返回值:定时器的ID值,可用于clearInterval()方法停止指定的定时器。

    注:setInterval()方法会不停地调用函数,直到用clearInterval()终止定时或窗口被关闭。

    window.clearInterval()

    功能:取消由setInterval()方法设置的定时器。

    语法:clearInterval(id_of_setinterval)

    解释:id_of_setinterval:由setInterval()返回的ID值。该值标识了一个setInterval定时器。

    也就是:window.setInterval()返回的就是window.clearInterval的参数

    例子:

    <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="开始计时" onclick="beginCount()" />
    <input type="text" id="timetxt" size="5" />
    <input type="button" value="停止计时" onclick="stopCount()" />
    再如:
    var objTimer = window.setInterval("moveDiv()",10)是调动定时器,其中moveDiv是js的一个函数

    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

  • 相关阅读:
    Shell 脚本学习 — 简单的执行跟踪
    CentOS — 安装Git客户端
    Linux — cat 命令的使用方法
    关于“分叉/联接方案”的一般做法
    读书笔记 —— 《MySQL技术内幕 InnoDB存储引擎》
    MySQL InnoDB 索引
    CentOS — MySQL备份 Shell 脚本
    CI system/libraries/Session.php
    WinForm 处理未处理的异常 Application.ThreadException + AppDomain.CurrentDomain.UnhandledException
    重构案例1 — ECShop (lib_common.php build_url 函数)
  • 原文地址:https://www.cnblogs.com/Defry/p/4588357.html
Copyright © 2011-2022 走看看