JS Timing

2016-02-19 09:54 28 1 收藏

今天给大家分享的是由图老师小编精心为您推荐的JS Timing,喜欢的朋友可以分享一下,也算是给小编一份支持,大家都不容易啊!

【 tulaoshi.com - Web开发 】

使用JS是可以让函数不直接执行的,而是在过了一个指定的时间间隔后才执行。这就叫做事件事件。
With JavaScript, it is possible to execute some code NOT immediately after a function is called, but after a specified time interval. This is called timing events.
使用JS是可以让函数不直接执行的,而是在过了一个指定的时间间隔后才执行。这就叫做事件事件。
JavaScript Timing Events
JS时间事件
With JavaScript, it is possible to execute some code NOT immediately after a function is called, but after a specified time interval. This is called timing events.
使用JS是可以让函数不直接执行的,而是在过了一个指定的时间间隔后才执行。这就叫做事件事件。
It's very easy to time events in JavaScript. The two key methods that are used are:
JS的时间事件是非常简单的。使用了两个关键的方法:
    * setTimeout() - executes a code some time in the future
      在一些时间后执行代码
    * clearTimeout() - cancels the setTimeout()
      取消setTimeout() 
Note: The setTimeout() and clearTimeout() are both methods of the HTML DOM Window object.
注意:setTimeout() 和 Timeout() 都是HTML DOM Window 对象的方法。
setTimeout()
Syntax语法
var t=setTimeout("javascript statement",milliseconds)
The setTimeout() method returns a value - In the statement above, the value is stored in a variable called t. If you want to cancel this setTimeout(), you can refer to it using the variable name.
setTimeout()方法返回一个值 - 在上面的声明里,值被保存在变量t中。如果你想取消这个setTimeout()可以使用变量名来提出它(用clearTimeout(t))
The first parameter of setTimeout() is a string that contains a JavaScript statement. This statement could be a statement like "alert('5 seconds!')" or a call to a function, like "alertMsg()".
setTomeout()的第一个参数是字符串声明。它可以像"alert('5 seconds!')"或是调用一个函数像"alertMsg()"
The second parameter indicates how many milliseconds from now you want to execute the first parameter.
第二个参数用来表明从现在开始你希望在多少毫秒后执行第一个参数
Note: There are 1000 milliseconds in one second.
1000毫秒为一秒
Example
举例
When the button is clicked in the example below, an alert box will be displayed after 5 seconds.
当下面的按钮被点击后,每过5秒就会出现一个警告框。
html
head
script type="text/javascript"
function timedMsg()
{
var t=setTimeout("alert('5 seconds!')",5000)
}
/script
/head
body
form
input type="button" value="Display timed alertbox!"
onClick="timedMsg()"
/form
/body
/html
Example - Infinite Loop
无限循环
To get a timer to work in an infinite loop, we must write a function that calls itself. In the example below, when the button is clicked, the input field will start to count (for ever), starting at 0:
要得到一个无限循环的记时器,我们必须写出一个自我调用的函数。下面的例子,当按钮按下后,输入框就会从0开始记数(永远的)
html
head
script type="text/javascript"
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}
/script
/head
body
form
input type="button" value="Start count!"
onClick="timedCount()"
input type="text" id="txt"
/form
/body
/html
clearTimeout()
Syntax语法
clearTimeout(setTimeout_variable)
Example
举例
The example below is the same as the "Infinite Loop" example above. The only difference is that we have now added a "Stop Count!" button that stops the timer:
下面的例子和上面的“无限循环”差不多。唯一的不同就是我们现在多了一个“停止记数”的按钮来停止记时器。
html
head
script type="text/javascript"
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}
function stopCount()
{
clearTimeout(t)
}
/script
/head
body
form
input type="button" value="Start count!"
onClick="timedCount()"
input type="text" id="txt"
input type="button" value="Stop count!"
onClick="stopCount()"
/form
/body
/html

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

来源:https://www.tulaoshi.com/n/20160219/1592631.html

延伸阅读
标签: Web开发
代码如下: //---------------------------------------------------------------  // Opacity Displayer, Version 1.0  // Copyright Michael Lovitt, 6/2002.  // Distribute freely, but please leave this notice intact.  //-...
标签: Web开发
方法一:最笨的,最容易理解的,且可以随意设置要排除的字符 ====================================================================== script language="javascript" function isChinese1(str){ var badChar ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; badChar += "abcdefghijklmnopqrstuvwxyz"; badChar += "0123456789"; badChar +...
标签: Web开发
解密是很简单的问题,方法: 编写一个a.html,内容如下: textarea name=id_code style='width:100%;height:500'/textarea script language=javascript src=mapbarapi.js/script 修改下载到本地的mapbarapi.js文件,把第316个字符开始的return p修改为id_code.value=p即可,现在浏览器打开a.html就可以看见解密后的JS代码。 由...
标签: Web开发
看到论坛上有人模仿alert 自己也写了一个 本来想模仿winapi里的MessageBox  但可惜js 不支持 阻塞模式 返回值只能用异步了。 支持 FF ie opera DOCTYPE 可以申明 也可以不申明 存在问题 在opera 里图层不能透明 对于页面内有iframe的也无法使用 在ie里无法遮住select的 代码如下: !DOCTYPE...
标签: Web开发
通过这个工具,js文件的大小至少能减少到原来的一半,也就是说压缩比在50%以上,而且可以防止js代码被抄袭,真可谓一举两得 下载地址:(需要php的支持)http://www.51files.com/?ZHTFAFWYJ6CLC3P7GEBQ 在线压缩地址:http://dean.edwards.name/packer/

经验教程

296

收藏

42

精华推荐

$()JS小技巧

$()JS小技巧

路的人生512

奇妙的js

奇妙的js

尚格明媛

js操作listbox

js操作listbox

多米诺狂少

微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部