How to implement multiple JavaScript effects in the same Web page _javascript tips

Source: Internet
Author: User
Tags setinterval

The examples in this article tell you how to implement multiple JavaScript effects in the same Web page. Share to everyone for your reference. The specific analysis is as follows:

In general, if there are two <script type= "Text/javascript" ></script> tags in the Web page, all Javascipt scripts will not take effect again, only one time <script Type= "Text/javascript" ></script> tags, however, multiple JavaScript effects are often required on the same Web page.

I. BASIC OBJECTIVES

Mount two JavaScript clocks in a Web page, one of which is the normal time to walk every 1 seconds, and the other is an abnormal clock that goes every 3 seconds, just to differentiate to illustrate how to implement multiple JavaScript effects in the same Web page. The effect is shown in the following illustration:

Second, the production process

Method One:

Copy Code code as follows:
<script type= "Text/javascript" >
function Clocka () {
var time = new Date (). toLocaleString ();
document.getElementById ("Clocka"). InnerHTML = time;
}
function A () {
Clocka ();
SetInterval ("Clocka ()", 1000);
}
function clockb () {
var time = new Date (). toLocaleString ();
document.getElementById ("clockb"). InnerHTML = time;
}
Function B () {
CLOCKB ();
SetInterval ("clockb ()", 3000);
}
</script>
<body onload= "A (), B ()" >
<div id= "Clocka" ></div>
<div id= "CLOCKB" ></div>
</body>

First of all to achieve the effect of writing to a function, function A (), B (), and then through the body of the onload, let it load the Web page immediately to load this function.
As for Clocka () and clockb (), they are rewritten based on the original JavaScript code. The JavaScript code that originally was in <body> is as follows:

Copy Code code as follows:
<script type= "Text/javascript" >
function Clock () {
var time = new Date (). toLocaleString ();
document.getElementById ("Clock"). InnerHTML = time;
}
SetInterval ("clock ()", 1000);
</script>

Method Two:

is to write type directly in <script> without writing type types, but this method has a certain delay, the effect is a load, if too many special effects, the result will be bad.

But the neat and intuitive nature of the code is a triumph over the above method.

The code is as follows:

Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script>
function Clocka () {
var time = new Date (). toLocaleString ();
document.getElementById ("Clocka"). InnerHTML = time;
}
function clockb () {
var time = new Date (). toLocaleString ();
document.getElementById ("clockb"). InnerHTML = time;
}
</script>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>twojs</title>

<body>
<script>
SetInterval ("Clocka ()", 1000);
</script>
<script>
SetInterval ("clockb ()", 3000);
</script>
<div id= "Clocka" ></div>
<div id= "CLOCKB" ></div>
</body>

The

wants this article to help you with your JavaScript programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.