Home > Web Front-end > JS Tutorial > Implementing a function after a certain period of time based on JavaScript_javascript skills

Implementing a function after a certain period of time based on JavaScript_javascript skills

WBOY
Release: 2016-05-16 15:25:47
Original
1076 people have browsed it

In actual needs, it may be necessary to stipulate that a function be executed after a specified time to achieve the desired purpose. This is also a timer effect. It happens that such a function setTimeout() has been given in js , let’s first introduce the sub-function

Usage:

Grammar:

Copy code The code is as follows:

setTimeout(code,interval)

This function can specify a specified code to be executed after the specified event. This code will only be executed once.

The setTimeout() function has two parameters. The first parameter specifies the execution code to be executed, and the second parameter specifies how long to execute the code, in milliseconds.

Code example:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>脚本之家</title>
<style type="text/css"> 
div{ 
 width:200px; 
 height:50px; 
 margin:0px auto; 
 background-color:green; 
 text-align:center; 
 line-height:50px; 
 color:red; 
} 
</style> 
<script type="text/javascript"> 
function tishi(){ 
 alert("大家好欢迎来到脚本之家"); 
} 
setTimeout(tishi,5000) 
</script> 
</head> 
<body> 
<div>五秒弹出一个对话框</div> 
</body> 
</html> 
Copy after login

The above code can specify that a dialog box will pop up after 5 seconds. The above uses the setTimeout() function to run the tishi() function after 5 seconds.

The above content is based on JavaScript to implement the relevant knowledge of executing a function after a certain period of time. Friends who are interested can learn together.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template