window.clearInterval与window.setInterval的用法( ) js工作笔记

window.clearInterval与window.setInterval的用法( ) js工作笔记

今天因为修改网站遇到了一些问题,需要用js来解决。所以也让自己学习了一些东西,现在把从网上找到的东西整理一下:

window.setInterval()

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

语法:setInterval(code,millisec) 

参数:    code:在定时时间到时要执行的JavaScript代码串(可以是函数)。

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

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

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

window.clearInterval()

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

语法:clearInterval(id_of_setinterval)

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

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

unction dibutu(){
 var width = document.getElementsByTagName('body')[0].clientWidth;
document.getElementById("yidiv").style.width=width;
document.getElementById("yiimg").width=width;}
window.setInterval("dibutu()",1);

 

 

jQuery.metadata.setType("attr","data");

   var metadata=jQuery("#persontest_info").metadata();

   var total_time = parseInt(metadata.total_time);

   var times = total_time * 60;

   var timeup = false;

   var syncTimer = function() {

      var sync_url = persontest_info_sync_url;

      var data = {};

      data.jstime = times;

      data.sheet_id = $('#sheetid').val();

      $.ajax({

         type:"GET",

         url: sync_url,

         data:data,

         dataType: "json",

         success:function(data) {

            if (data) {

               times = data;

            }

         }

      });

   };

   

   var timeObj = window.setInterval(function(){

      if (times <= 0) {

         persontestSubmit();

         window.clearInterval(timeObj);

         return;

      }

 

      // time text turn red!

      if (times <= 600 && timeup == false) {

         timeup = true;

         jQuery("#timeleft").addClass("timeup");

      }

 

      // sync time every 5 minute 

      if(times%300 == 0) {

         syncTimer();

      }

      var text = Math.floor(times/60)+"\u5206"+times%60+"\u79d2";

      $('#timeleft').html(text);

      times --;

   }, 1000);

   

   

 // persontest ajax submit

   function persontestSubmit() {

      $("#persontestform").ajaxSubmit({

         dataType: "json",

         beforeSubmit: function(data, form, options) {

            if (!showLoading("#persontestform", options, null, {button:true}))

               return false;

            $("#errormsg").hide();

         },

         success: function(data) {

            try {

               var retmsg = data;

               if (retmsg.error) {

                  $("#errormsg").jobUtil("showError", retmsg.error);

               }

               if (retmsg.success) {

                  $("#errormsg").jobUtil("showSuccess", '\u63d0\u4ea4\u6210\u529f').fadeIn('fast');

                  window.location = retmsg.redirect;

               }

            } catch(e) {

               return false;

            }

         }

      });

   }

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值