Home  >  Article  >  Web Front-end  >  How to set delay seconds in javascript

How to set delay seconds in javascript

WBOY
WBOYOriginal
2022-02-08 11:52:358012browse

In JavaScript, you can use the setTimeout() method to set the delay for a few seconds. This method is used to call a function or calculate an expression after a specified number of milliseconds. The syntax is "setTimeout(code to be executed, waiting for milliseconds)".

How to set delay seconds in javascript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How to set the delay for several seconds in javascript

setTimeout() is a method belonging to window. This method is used to call a function or calculate an expression after a specified number of milliseconds.

The syntax format can be the following two:

setTimeout(要执行的代码, 等待的毫秒数)
setTimeout(JavaScript 函数, 等待的毫秒数)

The example is as follows:

setTimeout("alert('这是3秒后出现的')", 3000 )

The output result after 3 seconds:

How to set delay seconds in javascript

The example is as follows:

<html>
<head>
<script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert(&#39;5 seconds!&#39;)",5000)
}
</script>
</head>
<body>
<form>
<input type="button" value="显示计时的消息框!" onClick = "timedMsg()">
</form>
<p>点击上面的按钮。5 秒后会显示一个消息框。</p>
</body>
</html>

Output result:

How to set delay seconds in javascript

##Output result after 5 seconds of clicking the button:

How to set delay seconds in javascript

Related recommendations:

javascript learning tutorial

The above is the detailed content of How to set delay seconds in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn