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

Execute Javascript a duration of time after the page load

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

<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=''\/>");
sustituir = sustituir.replace(/\[video\]https:\/\/www\.youtube\.com\/embed\/(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='https://www.youtube.com/embed/'><\/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/'><\/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/'><\/iframe>");
sustituir = sustituir.replace(/\[video\](http:\/\/player\.vimeo\.com\/video\/)(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='http://player.vimeo.com/video/'><\/iframe>");
document.getElementById(id).innerHTML = sustituir;} InsertarImagenVideo('comment-holder');
//]]>
    </script>

不酷,但工作:

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

使用 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=''\/>");
    sustituir = sustituir.replace(/\[video\]https:\/\/www\.youtube\.com\/embed\/(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='https://www.youtube.com/embed/'><\/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/'><\/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/'><\/iframe>");
    sustituir = sustituir.replace(/\[video\](http:\/\/player\.vimeo\.com\/video\/)(.*?)\[\/video\]/ig, "<iframe class='vid-comentarios' src='http://player.vimeo.com/video/'><\/iframe>");
    document.getElementById(id).innerHTML = sustituir;
    }
   //]]> 

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

    </script>