如何为JS重定向添加延迟

时间:2017-12-06 13:17:08

标签: javascript

如何为此代码添加5秒的时间延迟?

<script type="text/javascript"> 
if (document.location.href.indexOf('url-wording') > -1) { 
    document.location.href = 'http://www.google.com/'; 
}
</script>

1 个答案:

答案 0 :(得分:1)

if(document.location.href.indexOf('url-wording') > -1) {
  setTimeout(function() {
    document.location.href = 'http://www.google.com/';
  }, 5000); // 5000ms = 5s
}