JavaScript delay repetitive execution function llooprun.js_javascript technique

Source: Internet
Author: User
function Llooprun (sfuncloop,sfuncend,ndelay) {
Writen by http://fengyan.iecn.cn

Sfuncloop >> String type, JavaScript functions or statements that need to be executed repeatedly (multiple functions or statements, separate)
Sfuncend >> string, JavaScript function or statement used to abort a repeating action (Sfuncloop)
Ndelay >> Digital, repeat time interval (milliseconds)
var vintervalid = null;
var runstring = Sfuncloop;
var stopstring = sfuncend;
var delaytime = Ndelay;
var ncount = 0;//Number of executions//To facilitate testing, the line is commented out when applied
This._doloop = function () {
if (Vintervalid &&!eval (stopstring)) {
eval (runstring);
ncount++;//record number of executions//To be easy to test, comment out the line when applied
} else {
Window.clearinterval (Vintervalid);
Vintervalid = null;
}
document.getElementById ("Testcount"). InnerHTML = ncount;//output execution times//To facilitate testing, the line is commented out when applied
}
Window.clearinterval (Vintervalid);
Vintervalid = Window.setinterval (this._doloop,delaytime);
}

Several instance code:
Horizontal Reciprocating movement:

<title>llooprun.js Application Example: Horizontal reciprocating movement </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<meta name= "Author" content= "Cnlei, Maple Rock"/>
<style type= "Text/css" >
#IECN {Position:absolute;}
</style>
<script type= "Text/javascript" src= "Llooprun.js" ></script>
<body>
<p> execution times: <strong id= "Testcount" >0</strong></p>
<script type= "Text/javascript" >
<!--
function Chgpos (sid,n) {
var o = document.getElementById (sId);
O.style.left = (parseint (o.style.left) +n) + "px";
}

function Chgposstop (Sid,nmax) {
var o = document.getElementById (sId);
if (parseint (o.style.left) <0) {isreback = false;}
if (parseint (o.style.left) >nmax) {Isreback = true;}
if (isreback) {
Nnum=-math.abs (Nnum);
} else {
Nnum=math.abs (Nnum);
}
}

var nnum=10;
var isreback = false;
Llooprun ("Chgpos (Iecn,nnum);", "Chgposstop (iecn,600)", 20);
-->
</script>
</body>


Auto scale size:
<title>llooprun.js Application Example: Auto-Flex size </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<meta name= "Author" content= "Cnlei, Maple Rock"/>
<script type= "Text/javascript" src= "Llooprun.js" ></script>
<body>
<p> execution times: <strong id= "Testcount" >0</strong></p>
<script type= "Text/javascript" >
<!--

function Chgpos (sid,n) {
var o = document.getElementById (sId);
O.width = (parseint (o.width) +n);
}

function Chgposstop (Sid,nmax) {
var o = document.getElementById (sId);
if (parseint (o.width) <10) {isreback = false;}
if (parseint (o.width) >nmax) {Isreback = true;}
if (isreback) {
Nnum=-math.abs (Nnum);
} else {
Nnum=math.abs (Nnum);
}
return parseint (o.style.left) >nmax | | (parseint (O.style.top) >nmax-200);
}

var nnum=10;
var isreback = false;
Llooprun ("Chgpos (Iecn,nnum);", "Chgposstop (iecn,500)", 20);
-->
</script>
</body>


Vertical Reciprocating movement:
<title>llooprun.js Application Example: Vertical reciprocating movement </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<meta name= "Author" content= "Cnlei, Maple Rock"/>
<style type= "Text/css" >
#IECN {Position:absolute;}
</style>
<script type= "Text/javascript" src= "Llooprun.js" ></script>
<body>
<p> execution times: <strong id= "Testcount" >0</strong></p>
<script type= "Text/javascript" >
<!--
function Chgpos (sid,n) {
var o = document.getElementById (sId);
O.style.top = (parseint (o.style.top) +n) + "px";
}

function Chgposstop (Sid,nmax) {
var o = document.getElementById (sId);
if (parseint (o.style.top) <0) {isreback = false;}
if (parseint (o.style.top) >nmax) {Isreback = true;}
if (isreback) {
Nnum=-math.abs (Nnum);
} else {
Nnum=math.abs (Nnum);
}
return parseint (o.style.top) >nmax | | (parseint (O.style.top) >nmax-200);
}

var nnum=10;
var isreback = false;
Llooprun ("Chgpos (Iecn,nnum);", "Chgposstop (iecn,300)", 20);
-->
</script>
</body>


Gradient display (picture):
<title>llooprun.js Application Example: Gradient display effect </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<meta name= "Author" content= "Cnlei, Maple Rock"/>
<style type= "Text/css" >
Body{background: #080; color: #fff;}
#IECN {
Background: #fff;
Filter:alpha (opacity=10);
-moz-opacity:.10;
opacity:.10;
}
</style>
<script type= "Text/javascript" src= "Llooprun.js" ></script>
<body>
<p> execution times: <strong id= "Testcount" >0</strong></p>
<br/><br Refresh View Demo Effect again
<script type= "Text/javascript" >
<!--
function Chgopacity (sid,n) {
var o = document.getElementById (sId);
if (o.filters) {
O.filters[0]. Opacity = parseint (O.filters[0]. Opacity) + N;
} else {
o.style.opacity= eval (Document.defaultView.getComputedStyle (o,null). GetPropertyValue (opacity)) + (n*100/10000);
}
}

function Chgopacitystop (sId) {
var o = document.getElementById (sId);
if (o.filters) {
return parseint (O.filters[0]. Opacity) >=99;
} else {
Return eval (o.style.opacity) >=0.99;
}
}

Llooprun ("chgopacity (iecn,1);", "Chgopacitystop (IECN)", 20);
-->
</script>
</body>


Original: http://fengyan.iecn.cn/blog-html-do-showone-uid-35653-type-blog-itemid-2320.html

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.