9

SharePoint 添加自动结束的JavaScript计时器

 3 years ago
source link: https://www.cnblogs.com/jianyus/p/14419170.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

SharePoint 添加自动结束的JavaScript计时器

  前言

  最近,做项目的时候需要写JavaScript处理一些样式问题,不过,发现页面很多地方是异步加载的,页面加载的时候执行脚本不行。

  于是乎,就有了下面的解决方法。

  正文

  如下脚本,可以在一定条件下,暂停之前的循环方法。

  运行60次自动停止的方法:

var timesRun = 0;
var interval = setInterval(function(){
    timesRun += 1;
    if(timesRun === 60){    
        clearInterval(interval);    
    }
    //do whatever here..
}, 2000);

   运行一分钟自动停止的方法:

var startTime = new Date().getTime();
var interval = setInterval(function(){
    if(new Date().getTime() - startTime > 60000){
        clearInterval(interval);
        return;
    }
    
    //do whatever here..
}, 2000);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK