页面加载后的一段时间内执行Javascript

时间:2019-02-06 14:57:37

标签: javascript cdata pageload dom-events

页面加载后,我已经在使用此脚本来执行。

<script type='text/javascript'>

window.onload = function() {
  autoloadmore()
};

function autoloadmore() {
  var loadmoreClass = document.getElementsByClassName(&quot;loadmore&quot;)[0];
  var loadmoreChild = loadmoreClass.querySelector(&#39;a&#39;)

  if (loadmoreClass) {
    loadmoreChild.click();
  }
}
</script>

我也想添加此代码,并使其在页面加载后5秒钟执行。

<script>
//<![CDATA[

function InsertarImagenVideo(id) {
var IDelemento = document.getElementById(id),
sustituir = IDelemento.innerHTML;
sustituir = sustituir.replace(/\[img\](.[^\]]*)\[\/img\]/ig, "<img class='img-comentarios' src='$1'\/>");
sustituir = sustituir.replace(/\[video\]https:\/\/www\.youtube\.com\/embed\/(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='https://www.youtube.com/embed/$1'><\/iframe>");
sustituir = sustituir.replace(/\[video\](https:\/\/youtu\.be\/|https:\/\/www\.youtube\.com\/watch\?v\=)(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='https://www.youtube.com/embed/$2'><\/iframe>");
sustituir = sustituir.replace(/\[video\](http:\/\/youtu\.be\/|http:\/\/www\.youtube\.com\/watch\?v\=)(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='https://www.youtube.com/embed/$2'><\/iframe>");
sustituir = sustituir.replace(/\[video\](http:\/\/player\.vimeo\.com\/video\/)(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='http://player.vimeo.com/video/$2'><\/iframe>");
document.getElementById(id).innerHTML = sustituir;} InsertarImagenVideo('comment-holder');
//]]>
    </script>

2 个答案:

答案 0 :(得分:0)

不酷,但是可以工作:

window.onload = function() {
  var timeInMilliSeconds = 1000;
  setTimeout(function(){
      autoloadmore();
  }, timeInMilliSeconds)
};

答案 1 :(得分:0)

使用SetTimeout:

    <script type='text/javascript'>

    function autoloadmore() {
      var loadmoreClass = document.getElementsByClassName(&quot;loadmore&quot;)[0];
      var loadmoreChild = loadmoreClass.querySelector(&#39;a&#39;)

      if (loadmoreClass) {
        loadmoreChild.click();
      }
    }

   //<![CDATA[
    function InsertarImagenVideo(id) {
    var IDelemento = document.getElementById(id),
    sustituir = IDelemento.innerHTML;
    sustituir = sustituir.replace(/\[img\](.[^\]]*)\[\/img\]/ig, "<img class='img-comentarios' src='$1'\/>");
    sustituir = sustituir.replace(/\[video\]https:\/\/www\.youtube\.com\/embed\/(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='https://www.youtube.com/embed/$1'><\/iframe>");
    sustituir = sustituir.replace(/\[video\](https:\/\/youtu\.be\/|https:\/\/www\.youtube\.com\/watch\?v\=)(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='https://www.youtube.com/embed/$2'><\/iframe>");
    sustituir = sustituir.replace(/\[video\](http:\/\/youtu\.be\/|http:\/\/www\.youtube\.com\/watch\?v\=)(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='https://www.youtube.com/embed/$2'><\/iframe>");
    sustituir = sustituir.replace(/\[video\](http:\/\/player\.vimeo\.com\/video\/)(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='http://player.vimeo.com/video/$2'><\/iframe>");
    document.getElementById(id).innerHTML = sustituir;
    }
   //]]> 

    window.onload = function() {
      autoloadmore();
      //<![CDATA[
      setTimeout(function(){
        InsertarImagenVideo('comment-holder');
      },5000);
     //]]>
    };

    </script>