في Javascritp ، هناك وظيفتان خاصتان حول أجهزة ضبط الوقت ، وهما:
1. عهد التداخل: timename = setTimeout ("function () ؛" ، addertime) ؛
2. مؤقت ركوب الدراجات: timename = setInterval ("function () ؛" ، addertime) ؛
المعلمة الأولى "الدالة ()" هي الإجراء الذي يتم تنفيذه عند تشغيل الموقت. على سبيل المثال ، إذا ظهرت نوافذ تحذير ، يمكنك استبدال "الوظيفة () ؛"
"تنبيه (" نافذة التحذير الأولى! ") ؛ تنبيه (" نافذة التحذير الثاني! ") ؛"
يؤدي توقيت العد التنازلي إلى حدوث حدث بعد الوقت ، والموقت المتداول هو تشغيل الحدث مرارًا وتكرارًا عند قدوم الفاصل الزمني.
على سبيل المثال ، بعد فتح صفحة وتريد القفز تلقائيًا إلى فاصل صفحة آخر في بضع ثوانٍ ، تحتاج إلى استخدام مؤقت العد التنازلي "setTimeout (" function () ؛ "، وقت التأخير) ، وإذا كنت ترغب في تعيين جملة إعدادات ظهور كلمة واحدة ،
تحتاج إلى استخدام المؤقت الدائري "setInterval (" function () ؛ "، وقت التأخير".
للحصول على تركيز النموذج ، يتم استخدام المستند. استخدم إذا كان لتحديد ما إذا كانت المستند.
على سبيل المثال: if ("mid" == document.activeElement.id) {Alert () ؛} ، "mid" هو المعرف المقابل للنموذج.
مؤقت:
تستخدم لتحديد برنامج معين بعد وقت محدد.
الاختلافات بين setTimeOut و SetInterval في JS ، وطريقة Llee
setTimeout (التعبير ، وقت التأخير).
setTimeout ("الوظيفة" ، الوقت) قم بتعيين كائن مهلة
SetInterval (التعبير ، وقت التأخير) ، سيتم تنفيذ كل تأخير Expdition.
SetInterval ("Function" (Time) يعين كائن مهلة
يتم تكرار SetInterval تلقائيًا ، ولن يتكرر SetTimeout.
ClearTimeout
Clearinterval
حالتان.
مثال 1. عندما يتم تشغيل النموذج أو تحميله ، يتم إخراج سلسلة الكلمة بالكلمة
نسخ رمز رمز على النحو التالي:
<html>
<head>
<meta http-equiv = "content-type" content = "text /html ؛ charset = gb2312" /> />
<title> وثيقة غير متوفرة </title>
<script language = "javaScript" type = "text/javaScript">
var str = "هذا هو النص على النص للاختبار" ؛
var seq = 0 ؛
var second = 1000 ؛
وظيفة Scroll () {
msg = str.substring (0 ، seq+1) ؛
document.getElementByidx_x_x ('Word').
seq ++ ؛
if (seq> = str.length) seq = 0 ؛
}
</script>
</head>
<body onload = "setInterval ('scroll ()' ، sex)">
<div id = "word"> </viv> <br/> <br/>
</body>
</html>
مثال 2. عندما يكون التركيز في مربع الإدخال ، حدد معلومات مربع الإدخال بانتظام.
نسخ رمز رمز على النحو التالي:
<!
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "content-type" content = "text /html ؛ charset = gb2312" /> />
<title> وثيقة غير متوفرة </title>
<script language = "javaScript" type = "text/javaScript">
VAR Second = 5000 ؛
var c = 0 ؛
وظيفة Scroll () {
C ++ ؛
if ("b" == document.activelement.id) {{
var str = "توقيت تحقق من <b>"+c+"</b> مرات <br/>" ؛
if (document.getElementByIDX_X_X ('b'). value! = "") {
STR+= "مربع الإدخال هو المحتوى الحالي هو <br/> <b>"+dcument.getElementByidx_x_x ('B').
}
document.getElementByidx_x_x ('Word').
}
}
</script>
</head>
<Body>
<textarea id = "b" name = "b" style = "الارتفاع: 100px ؛ العرض: 300px ؛" BR /> "
<div id = "word"> </viv> <br/> <br/>
</body>
</html>
مثال 3. ما يلي هو أبسط مثال.
نسخ رمز رمز على النحو التالي:
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "content-type" content = "text /html ؛ charset = gb2312" /> />
<script language = "javaScript">
عدد الوظائف () {) {
document.getElementByidx_x_x ('m').
setTimeout ("تنبيه ('عشر ثوانٍ إلى!" ، 10000)
}
</script>
<Body>
<div id = "m"> </viv>
<type type = "button" value = "time start" onClick = "count ()">
</body>
</html>
مثال 4: العد التنازلي يقفز التوقيت وفي الوقت المناسب
نسخ رمز رمز على النحو التالي:
<html>
<head>
<base href = "<٪ = basepath ٪>">
<title> بلدي JSP 'DS04.JSP' صفحة البدء </title>
<span id = "tiao"> 3 </span>
<a href = "javaScript: Countdown"> </a> القفز تلقائيًا بعد الثانية ...
<meta http-equiv = تحديث المحتوى = 3 ؛
<!-البرنامج النصي يبدأ->
<script language = "javaScript" type = "">
وظيفة العد التنازلي (Secs) {
tiao.innertext = secs ؛
إذا (-SCS> 0)
setTimeout ("العد التنازلي ("+secs+") ، 1000) ؛ 1000) ؛
}
العد التنازلي (3) ؛
</script>
<!-Script End->
</head>
مثال 6:
نسخ رمز رمز على النحو التالي:
<head>
<meta http-equiv = "refresh" content = "2 ؛ url = 'b.html'">
</head>
مثال 7:
نسخ رمز رمز على النحو التالي:
<script language = "javaScript" type = "text/javaScript">
setTimeout ("window.location.href = 'b.html'" ، 2000) ؛
// يمكن استخدام كلا التاليين
// setTimeout 18 "JavaScript: lcation.href = 'B.Html'" ، 2000) ؛
//settimeout.com window.location = 'b.html' "، 2000) ؛
</script>
مثال 8:
نسخ رمز رمز على النحو التالي:
<span id = "totalsecond"> 2 </span>
<script language = "javaScript" type = "text/javaScript">
var second = document.getElementByidx_x ('totalsecond').
if (isnan (الثاني)) {{
// ... ليس عدد الأرقام
} آخر {
setInterval (function () {
document.getElementByidx_x ('totalsecond').
if (الثاني <= 0) {{
window.location = 'b.html' ؛
}
} ، 1000) ؛
}
</script>
Timer JS (تنفيذ مرة واحدة ، كرر التنفيذ)
شارك رمز JS ، وهو مثال صغير على مؤقت JS ، مقسومًا إلى مؤقت وتوقيت التنفيذ المتكرر للتنفيذ. مرجع للأصدقاء المبتدئين.
1. فقط تنفيذ المؤقت مرة واحدة
نسخ رمز رمز على النحو التالي:
<script>
// الموقت يعمل غير متزامن
وظيفة مرحبا () {
تنبيه ("مرحبا") ؛
}
// استخدم طريقة تنفيذ اسم الطريقة
var t1 = window.settimeout (Hello ، 1000) ؛
var t2 = window.settimeout ("Hello ()" ، 3000) ؛
window.cleartimeout (T1) ؛
</script>
2. مؤقت التنفيذ المتكرر
نسخ رمز رمز على النحو التالي:
<script>
وظيفة مرحبا () {
تنبيه ("مرحبا") ؛
}
// كرر طريقة معينة
var t1 = window.setInterval (Hello ، 1000) ؛
var t2 = window.setInterval ("Hello ()" ، 3000) ؛
// طريقة إزالة المؤقت
window.clearinterval (T1) ؛
</script>
ملاحظة:
إذا كانت هناك طريقتين في صفحة واحدة ، يتم تنفيذها بعد تحميل الصفحة ، ولكن يمكن تنفيذ الفشل الفعلي بالترتيب.
يمكنك إضافة مؤقت إلى الطريقة عبر الإنترنت ، وتعيين مؤقت ، ثم تشغيله بعد "تأخير" لفترة من الوقت.
نسخ رمز رمز على النحو التالي:
<!
<html xmlns = "http://www.w3.org/1999/xhtml">
<head runat = "server">
<title> صفحة بدون عنوان </title>
<script language = "javaScript" type = "text/javaScript">
var yc = new Object () ؛
وظيفة beginyc ()
{{
var secondsyc = document.getElementById ("txtycseconds").
يحاول
{{
yc = setTimeout ("Alert ('Delay"+SecondSyc+"Second Success') ، SecondSyc*1000) ؛
}
الصيد (ه)
{{
تنبيه ("الرجاء إدخال الرقم الثاني الصحيح.") ؛
}
}
وظيفة overyc ()
{{
ClearTimeout (YC) ؛
yc = null ؛
تنبيه ("تأخير الإنهاء الناجح") ؛
}
/************************ ↓ ↓ 定时器的使用 ↓ ↓ ↓ *********** ****************************
توقيت var = كائن جديد () ؛
var timerdds = new Object () ؛
الوظيفة beginds ()
{{
sn.innerhtml = "0" ؛
توقيت = setInterval ("addone ()" ، parseint (document.getElementById ("txtintirvalseconds"). القيمة 10)*1000) ؛
}
وظيفة goonds ()
{{
توقيت = setInterval ("addone ()" ، parseint (document.getElementById ("txtintirvalseconds"). القيمة 10)*1000) ؛
}
وظائف Outds ()
{{
ClearInterval (توقيت) ؛
توقيت = فارغ ؛
}
تأخير الوظيفة ()
{{
overds () ؛
TimerDds = setTimeOut ("goonds ()" ، document.getElementById ("txtddsseconds"). value*1000) ؛
}
وظيفة addone ()
{{
if (sn.innerhtml == "10")
{{
overds () ؛
في حالة تأهب ("تهانينا ، لقد وصل بنجاح إلى 10 ثوان") ؛
يعود؛
}
sn.innerhtml = parseint (sn.innerhtml ، 10) +1 ؛
}
</script>
</head>
<Body>
<found id = "form1" runat = "server">
<viv>
استخدام التأخير: </div>
<viv>
<label id = "label2"> </billy>
<type type = "text" id = "txtycseconds" value = "3" />
<type type = "button" id = "btnbyc" onClick = "javaScript: beginyc ()" value = "start delay" />
<type type = "button" id = "btnoyc" onClick = "javaScript: Overyc ()" value = "termid delay" />
<type type = "button" id = "button1" onClick = "javaScript: Alert ('monrning Good') ؛
</div>
<br />
<viv>
استخدام المؤقت: </div>
<viv>
<div id = "sn"> 0 </viv>
<label id = "label1"> </billy>
<type type = "text" id = "txtiintervalseconds" value = "1" /> />
<type type = "button" id = "btnbds" onClick = "javaScript: beggs ()" value = "start time" />
<type type = "button" id = "btnods" onClick = "javaScript: overds ()" value = "terminal time" /> />
<type type = "button" id = "btngds" onClick = "javaScript: goonds ()" value = "Contert timing" />
<laabel id = "ds"> </billy>
<type type = "text" id = "txtddsseconds" value = "5" />
<type type = "button" id = "btndds" onClick = "javaScript: delayds ()" value = "تأخير توقيت" />
</div>
</form>
</body>
</html>