<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>setInterval &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/setinterval/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Tue, 22 Jan 2013 06:23:51 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>
	<item>
		<title>Javascript的setInterval和clearInterval应用</title>
		<link>https://www.mudbest.com/javascript%e7%9a%84setinterval%e5%92%8cclearinterval%e5%ba%94%e7%94%a8/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Tue, 22 Jan 2013 06:17:43 +0000</pubDate>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[clearInterval]]></category>
		<category><![CDATA[setInterval]]></category>
		<category><![CDATA[倒计时]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1753</guid>

					<description><![CDATA[时间间隔可以用 setInterval 命令来创建并用 clearInterval 命令来终止。setInte ... <a title="Javascript的setInterval和clearInterval应用" class="read-more" href="https://www.mudbest.com/javascript%e7%9a%84setinterval%e5%92%8cclearinterval%e5%ba%94%e7%94%a8/" aria-label="阅读 Javascript的setInterval和clearInterval应用">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>时间间隔可以用 setInterval 命令来创建并用 clearInterval 命令来终止。setInterval 所用的参数有两种格式。在第一种格式中，你传递给 setInterval 的参数可以是一个函数名，一段时间上的间隔以及一些传递给前面函数的相关参数。当 setInterval 运行时它会依照规定的时间间隔依次将列出的参数传递给指定的函数，直到你调用 clearInterval 将其终止。相关的示范代码<span id="more-1753"></span>如下： </p>
<pre class="brush: jscript; title: ; notranslate">
function updateStockPrices(whichStock) { 
    // Update code here 
    trace('Updating prices for '+whichStock); 
} 
stockInterval = setInterval(updateStockPrices, 1000, &quot;Stratford Flash Products&quot;);
</pre>
<p>setInterval全面的介绍<br />
setInterval动作的作用是在播放动画的时，每隔一定时间就调用函数，方法或对象。可以使用本动作更新来自数据库的变量或更新时间显示。setInterval动作的语法格式如下： </p>
<p>setInterval(function,interval[,arg1,arg2,&#8230;&#8230;argn])<br />
setInterval(object,methodName,interval[,arg1,arg2,&#8230;..argn]) </p>
<p>第一种格式是标准动作面板中setInterval函数的默认语法，第二种格式是在专家模式动作中使用的方法。<br />
其中的参数function是一个函数名或者一个对匿名函数的引用。object参数指定从Object对象派生的对象。methodName制定object参数中要调用的方法。interval制定对function或methodName调用两次之间的时间，单位是毫秒。后面的arg1等是可选的参数，用于制定传递给function或是methodName的参数。<br />
setInterval它设置的时间间隔小于动画帧速（如每秒10帧，相当于100毫秒），则按照尽可能接近interval的时间间隔调用函数。而且必须使用updateAfterEvent动作来确保以足够的频率刷新屏幕。如果interval大于动画帧速，则只用在每次播放头进入某一帧是才调用，以减小每次刷新屏幕的影响。<br />
下面的例子每隔1秒调用一次匿名函数。 </p>
<p>setInterval(function(){trace(&#8220;每隔1秒钟我就会显示一次&#8221;)},1000);//这里的function(){}是没有函数名<br />
的函数。成为匿名函数，后面的1000是时间间隔，单位是毫秒。<br />
下面的例子为我们展示如何带参数运行。 </p>
<pre class="brush: jscript; title: ; notranslate">
function show1(){ 
    trace(&quot;每隔1秒我就会显示一次&quot;); 
} 
function show2(str){ 
    trace(str); 
} 
setInterval(show1,1000); 
</pre>
<p>setInterval(show2,2000,&#8221;每隔2秒我就会显示一次&#8221;);<br />
上面已经将函数的setInterval方法介绍了。<br />
下面我们将介绍对象的setInterval方法。<br />
首先，写一个setInterval在动作中调用对象的方法的例子，该例子不需要传递参数。 </p>
<pre class="brush: jscript; title: ; notranslate">
myobj=new Object();//创建一个新的对象 
myobj.interval=function){ 
trace(&quot;每隔1秒我就会显示一次&quot;); 
}//创建对象的方法。 
setInterval(myobj,&quot;interval&quot;,1000);//设定时间间隔调用对象的方法。 
</pre>
<p>接下来介绍如何传递参数。其实道理和函数的传递参数是一样的。 </p>
<pre class="brush: jscript; title: ; notranslate">
myobj=new Object(); 
myobj.interval-function(str){ 
    trace(str); 
} 
setInterval(myobj,&quot;interval&quot;,2000,&quot; 每隔2秒我就会显示一次&quot;); 
</pre>
<p>注意。要调用为对象定义的方法时，必须在专家模式中使用第二种语法格式。<br />
这样子的话呢，我们来作一个动态显示时间的画面。可以用下面的代码实现。 </p>
<pre class="brush: jscript; title: ; notranslate">
setInterval(show,1000); 
function show(){ 
    time=new Date(); 
    hour=time.getHours(); 
    minu=time.getMinutes(); 
    sec=time.get.Seconds(); 
    datetime=hour &quot;:&quot; minu &quot;:&quot; sec; 
}
//这里的datetime是一个动态文本框的变量名字。 
</pre>
<p>这样子呢，setInterval这个方法大家应该学的很好了。现在呢，我们学习clearInterval.<br />
clearInterval动作的作用是清楚对setInterval函数的调用，它的语法格式如下clearInterval(intervalid);intervalid是调用setInterval函数后返回的对象。<br />
下面举一个简单的例子。 </p>
<pre class="brush: jscript; title: ; notranslate">
function show(){ 
    trace(&quot;每隔一秒我就会显示一次&quot;); 
} 
var sh; 
sh=setInterval(show,1000); 
clearInterval(sh); 
</pre>
<p>JS Demo:</p>
<pre class="brush: jscript; title: ; notranslate">
function auto(){ 
alert(&quot;到時間了&quot;) 
} 
var monitorInterval = null;  
function setAuto(time,isFrist){ 
    var intervalTime=time; 
        if(isFrist!=&quot;1&quot;){ 
            if(intervalTime!=&quot;off&quot;){ 
                monitorInterval= setInterval(&quot;auto()&quot;, intervalTime*1000); 
            }else{ 
            if(monitorInterval){    
                clearInterval(monitorInterval); 
                monitorInterval = null; 
            } 
        } 
    } 
} 
</pre>
<p>Html Demo:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;table&gt; 
&lt;tr&gt; 
&lt;td nowrap=&quot;nowrap&quot; bgcolor=&quot;#E8E8E8&quot;&gt;自動更新&lt;/td&gt; 
&lt;td align=&quot;left&quot; bgcolor=&quot;#E8E8E8&quot;&gt;&lt;select 
onchange=&quot;setAuto(this.value,'0')&quot;&gt; 
&lt;option value=&quot;10&quot;&gt;10sec&lt;/option&gt; 
&lt;option value=&quot;20&quot;&gt;20sec&lt;/option&gt; 
&lt;option value=&quot;30&quot;&gt;30sec&lt;/option&gt; 
&lt;option value=&quot;60&quot;&gt;1min&lt;/option&gt; 
&lt;option value=&quot;300&quot;&gt;5min&lt;/option&gt; 
&lt;option value=&quot;600&quot;&gt;10min&lt;/option&gt; 
&lt;option value=&quot;1800&quot;&gt;30min&lt;/option&gt; 
&lt;option value=&quot;3600&quot;&gt;60min&lt;/option&gt; 
&lt;option value=&quot;off&quot;&gt;Stay&lt;/option&gt; 
&lt;/select&gt;&lt;/td&gt; 
&lt;/tr&gt; 
&lt;/table&gt;
</pre>
<p>另一片文章也讲到了setInterval和clearInterval。</p>
<blockquote class="wp-embedded-content" data-secret="mxjtGy9BkW"><p><a href="https://www.mudbest.com/js%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0%e6%97%b6%e9%97%b4%e6%a0%bc%e5%bc%8f%e5%80%92%e8%ae%a1%e6%97%b6/">js简单实现时间格式倒计时</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="《js简单实现时间格式倒计时》—影子" src="https://www.mudbest.com/js%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0%e6%97%b6%e9%97%b4%e6%a0%bc%e5%bc%8f%e5%80%92%e8%ae%a1%e6%97%b6/embed/#?secret=mxjtGy9BkW" data-secret="mxjtGy9BkW" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
