Home >Web Front-end >JS Tutorial >What is a js timer? How to use js timer

What is a js timer? How to use js timer

云罗郡主
云罗郡主Original
2018-11-08 13:51:578890browse

Many people will encounter the carousel effect of pictures and play it for two minutes. At this time, a timer will be needed. So what is a js timer? Let's explain how to use js timer.

1: What is a js timer

js timer is a method of using js to achieve timing. There are many uses for timers on websites. , the production of many online clocks, the implementation of picture carousels, and some advertising pop-ups, anything that can be executed automatically can be related to timers.

2: Two methods used by js timer

(1)setTimeout() and clearTimeout();

In js, we You can use setTimeout() and clearTimeout to set the function, and the function is called once and cannot be executed repeatedly. ClearTimeout() can cancel the execution. The syntax is:

var 变量名 = setTimeout(code , time);

The above syntax is to call a function with a variable Name, we can set the time to indicate how long it will take before the content in the code will be automatically automatically generated.

For example:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            setTimeout("alert(&#39;php中文欢迎你&#39;);", 2000);
        }
    </script>
</head>
<body>
    <p>php中文欢迎你</p>
</body>
</html>

The display effect is as follows:

What is a js timer? How to use js timer

(2) setInterval() and clearInterval();

In js, we can use setInterval() to call a function. The syntax is:

var 变量名 = setInterval (code , time);

setInterval will only execute a piece of code repeatedly, and setTimeout() can only be executed once.

The above is about what is a js timer? A complete introduction to how to use js timer. If you want to know more about JavaScript tutorial, please pay attention to the PHP Chinese website.


The above is the detailed content of What is a js timer? How to use js timer. 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

Related articles

See more