關(guān)于JS控制代碼暫停的實現(xiàn)方法分享
更新時間:2012年10月11日 23:08:46 作者:
關(guān)于JS控制代碼暫停的工作總結(jié),需要的朋友可以參考下
方法一:這是在網(wǎng)上找的一個方法,可以用。但說實話,這個方法我不怎么明白。。。寫得好復雜。這樣做跟setTimeout能有多大區(qū)別?
function Pause(obj, iMinSecond) {
if (window.eventList == null ) window.eventList = new Array();
var ind = -1;
for (var i = 0; i < window.eventList.length; i++) {
if (window.eventList[i] == null ) {
window.eventList[i] = obj;
ind = i;
break;
}
}
if (ind == -1) {
ind = window.eventList.length;
window.eventList[ind] = obj;
}
setTimeout( "GoOn(" + ind + ")" , iMinSecond);
}
function GoOn(ind) {
var obj = window.eventList[ind];
window.eventList[ind] = null;
if (obj.NextStep) obj.NextStep();
else obj();
}
function testJsStop() {
alert( "1");
Pause( this, 3000);
this.NextStep = function () {
alert( "2");
}
}
方法二:這也是在網(wǎng)上找的,可以用。它的原理是先彈出一個窗口,因為JS在彈出窗口時,代碼會在當前位置暫停執(zhí)行。等過了一段時間后再執(zhí)行關(guān)閉窗口函數(shù),代碼繼續(xù)執(zhí)行。這中方法非常簡單,但令人討厭的是它會彈出一個窗口。。。
function pause(numberMillis) {
addcloud();
var dialogScript = 'window.setTimeout(' + ' function () { $("#bgDiv").remove(); }, ' + numberMillis + ');';
var result = window.showModalDialog('javascript:document.writeln(' + '"<script>' + dialogScript + '<' + '/script>")' );
}
function test() {
var a = 0;
alert(a);
pause(5000);
a = 999;
alert(a);
}
方法三:這個方法是我自己寫的。因為我要實現(xiàn)的功能比較復雜,要循環(huán)調(diào)用getpath()方法。而前面的兩種方法都只能應用在順序執(zhí)行的代碼段中,無法控制循環(huán)。在這里我采用了前后臺結(jié)合的方法。在前臺通過Ajax調(diào)用后臺方法,直接將線程掛起1s,成而實現(xiàn)JS代碼強制暫停。
前臺JS:
function getpath() {
var time = 1000;
$.ajaxSettings.async = false;
$.getJSON( "../Actions/TspHandler.ashx?rKey=" + parseInt(Math.random() * 999 + 1).toString() + "&opKey=Sleep"
+ "&Time=" + time,
null,
function (json) {
});
..........
}
后臺ashx:
if (methodname == "Sleep" )//休眠
{
int time = int .Parse(req["Time"].ToString());
System.Threading. Thread.Sleep(time);
}
以上僅供大家參考,歡迎吐槽!
復制代碼 代碼如下:
function Pause(obj, iMinSecond) {
if (window.eventList == null ) window.eventList = new Array();
var ind = -1;
for (var i = 0; i < window.eventList.length; i++) {
if (window.eventList[i] == null ) {
window.eventList[i] = obj;
ind = i;
break;
}
}
if (ind == -1) {
ind = window.eventList.length;
window.eventList[ind] = obj;
}
setTimeout( "GoOn(" + ind + ")" , iMinSecond);
}
function GoOn(ind) {
var obj = window.eventList[ind];
window.eventList[ind] = null;
if (obj.NextStep) obj.NextStep();
else obj();
}
function testJsStop() {
alert( "1");
Pause( this, 3000);
this.NextStep = function () {
alert( "2");
}
}
方法二:這也是在網(wǎng)上找的,可以用。它的原理是先彈出一個窗口,因為JS在彈出窗口時,代碼會在當前位置暫停執(zhí)行。等過了一段時間后再執(zhí)行關(guān)閉窗口函數(shù),代碼繼續(xù)執(zhí)行。這中方法非常簡單,但令人討厭的是它會彈出一個窗口。。。
復制代碼 代碼如下:
function pause(numberMillis) {
addcloud();
var dialogScript = 'window.setTimeout(' + ' function () { $("#bgDiv").remove(); }, ' + numberMillis + ');';
var result = window.showModalDialog('javascript:document.writeln(' + '"<script>' + dialogScript + '<' + '/script>")' );
}
function test() {
var a = 0;
alert(a);
pause(5000);
a = 999;
alert(a);
}
方法三:這個方法是我自己寫的。因為我要實現(xiàn)的功能比較復雜,要循環(huán)調(diào)用getpath()方法。而前面的兩種方法都只能應用在順序執(zhí)行的代碼段中,無法控制循環(huán)。在這里我采用了前后臺結(jié)合的方法。在前臺通過Ajax調(diào)用后臺方法,直接將線程掛起1s,成而實現(xiàn)JS代碼強制暫停。
前臺JS:
復制代碼 代碼如下:
function getpath() {
var time = 1000;
$.ajaxSettings.async = false;
$.getJSON( "../Actions/TspHandler.ashx?rKey=" + parseInt(Math.random() * 999 + 1).toString() + "&opKey=Sleep"
+ "&Time=" + time,
null,
function (json) {
});
..........
}
后臺ashx:
復制代碼 代碼如下:
if (methodname == "Sleep" )//休眠
{
int time = int .Parse(req["Time"].ToString());
System.Threading. Thread.Sleep(time);
}
以上僅供大家參考,歡迎吐槽!
相關(guān)文章
.NET微信公眾號開發(fā)之創(chuàng)建自定義菜單
這篇文章主要介紹了.NET微信公眾號開發(fā)之創(chuàng)建自定義菜單的相關(guān)資料,需要的朋友可以參考下2015-07-07
JavaScript+html實現(xiàn)前端頁面滑動驗證(2)
這篇文章主要為大家詳細介紹了JavaScript+html實現(xiàn)前端頁面滑動驗證的第二種方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
JavaScript常見的函數(shù)中的屬性與方法總結(jié)
當定義和調(diào)用函數(shù)時,JavaScript?函數(shù)對象會自動具有一些特定的屬性,本文為大家總結(jié)了一些常見的屬性和方法,感興趣的小伙伴可以了解一下2023-05-05
微信小程序bindtap與catchtap的區(qū)別詳解
本文主要介紹了微信小程序bindtap與catchtap的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
在JavaScript中查找字符串中最長單詞的三種方法(推薦)
這篇文章主要介紹了在JavaScript中查找字符串中最長單詞的三種方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01

