跳转至

Javascript

简单的js倒计时代码

$(function () function countdown(dateString, callback) 
 var timestamp = Date.parse(dateString), timer;
 function timeDiff() 
 var t = timestamp - (new Date).getTime(),
 d = time: t, day: '00', hour: '00', min: '00', sec: '00',
 s = '';
 if (t > 0) 
 s = '0' + parseInt(t / 1000 % 60);
 d.sec = s.substr(s.length - 2); //秒
 s = '0' + parseInt(t / 60000 % 60);
 d.min = s.substr(s.length - 2); //分
 s = '0' + parseInt(t / 3600000 % 24);
 d.hour = s.substr(s.length - 2); //时
 s = '0' + parseInt(t / 86400000 % 30);
 d.day = s.substr(s.length - 2); //天

 return d;

 timer = setInterval(function () 
 var result = timeDiff();
 result.time > 0 ? callback(result) : clearInterval(timer);
 , 1000);
 callback(timeDiff());

 var day = $('#theDay'), hour = $('#theHour'), min = $('#theMin'), sec = $('#theSec'),
 endTime = '2012/12/21 00:00:00';
 countdown(endTime, function (d) 
 day.text(Number(d.day));
 hour.text(d.hour);
 min.text(d.min);
 sec.text(d.sec);
 );
); 

简单的WEB Worker示例代码

  • Requirement: a web server, here we use python to run a simple web server

python.exe -m SimpleHTTPServer 8080
#For python3, use this command:
python3 -m http.server 8080
- The HTML, master code

<!DOCTYPE HTML>
<html>
  <head>
      <title>WEB Workder DEMO</title>
      <script>
      //Add version number to avoid cache;
      let workerJsFile = "worker.js?version=" + Date.now();
      var worker = new Worker(workerJsFile);

      worker.onmessage = function (event) {
          document.getElementById("content").innerHTML = event.data;
      };

      function startTimer() {
          worker.postMessage("START");
      }

      function stopTimer() {
          worker.postMessage("STOP");
      }
</script>
      <style type="text/css">
table {
  border-collapse:true;
  border-spacing:0;
}
thead {
  background: lightgray;
}
</style>
  </head>
  <body>
      <table border="1px">
          <thead>
              <tr>
                  <th>Current Time</th>
              </tr
          </thead>
          <tbody>
              <tr>
                  <td id="content">

</td>
              </tr>
          </tbody>
          <tfoot>
              <tr>
                  <td>
                      <button id="start" onclick="startTimer()">Start</button>
                      <button id="stop" onclick="stopTimer()">Stop</button>
                  </td>
              </tr>
          </tfoot>
      </table>
  </body>
</html>
- The JS, worker code

var runningTimer;
//await must in async function;
async function report() {
    //const d = new Date();
    var strPost = "Fail to get time from server";
    const timeServer = "http://worldtimeapi.org/api/timezone/Asia/Shanghai";
    let response = await fetch(timeServer);
    if (response.ok) {
        let json = await response.json();
      strPost=json.datetime;
    }
    this.postMessage(strPost);
}
this.addEventListener("message", function (event) {
    let cmd = event.data;
    switch (cmd) {
    case "START":
        this.postMessage("Started");
        clearInterval(runningTimer);
        runningTimer = setInterval(report, 1000);
        break;
    case "STOP":
        this.postMessage("Stopped");
        clearInterval(runningTimer);
        break;
    }

}, false);

WPS JS插件实现多顶占矩形

 function CreateDialog_CommandButton1_Click()

{

    let nodesAdd=CreateDialog.SpinButton1.Value-4;

    let linesAdd=nodesAdd==0?0:nodesAdd/2;

    let myDocument = Application.ActivePresentation.Slides.Item(1)

    let theShape=myDocument.Shapes.AddShape(msoShapeRectangle, 50, 50, 200, 20)

    theShape.TextFrame.TextRange.Text="New Shape"

    var subShapes=new Array();

    subShapes.push(theShape.Name);

    var subL=theShape.Left;

    var subT=theShape.Top;

    var subH=theShape.Height

    let subGap=theShape.Width/(linesAdd+1);

    for(i=1;i<linesAdd+1;i++){

        subL+=subGap;

        var shape1=myDocument.Shapes.AddShape(msoShapeRectangle,subL,subT,1,subH);

        shape1.Fill.Transparency=1.0;

        shape1.Line.Transparency=1.0;

        subShapes.push(shape1.Name);

    }

    if(linesAdd>0){

        myDocument.Shapes.Range(subShapes).Group();

    }

   CreateDialog.Close();

}