惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
D
Docker
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
F
Full Disclosure
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
L
LangChain Blog
H
Help Net Security
B
Blog
T
Tailwind CSS Blog
V
V2EX
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
美团技术团队
A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
I
InfoQ
Project Zero
Project Zero
I
Intezer
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Securelist
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 叶小钗
S
Security Affairs
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
The Hacker News
The Hacker News

博客园 - 懒蜜蜂

身份证校验规则 [转]扩展jQuery easyui datagrid增加动态改变列编辑的类型 在IIS 5.1/6上以FastCGI方式安装 PHP 如何修复.Net和COM之间枚举名称的问题(续) 通过修改IP/TCP包头伪造IP来与网页进行交互 如何修复.Net和COM之间枚举名称的问题 Compiling and Registering a Type Library Objective-C札记二-- Iphone中关于Layer的opaque属性 Xcode配置SVN作为版本控制 Emacs快捷键 Objective C中的札记--字符串连接,@selector中的冒号,时间转换,局部变量 使用GDataXML的设置 IPhone Exec_Bad_Access问题解决办法 C#版的AMF消息封装 数据库基础理论--关系模式及范式 函数依赖(转) 数据库基础理论之--超键 候选键 主键 拷贝VMWare虚机(XP操作系统)之后网络不通
Javascript中暂停功能的实现 - 懒蜜蜂 - 博客园
懒蜜蜂 · 2009-11-10 · via 博客园 - 懒蜜蜂

    方法一:使用setTimeOut来实现

    ==========================================================

    <script language="javascript">
    /*Javascript中暂停功能的实现
    Javascript本身没有暂停功能(sleep不能使用)同时 vbscript也不能使用doEvents,故编写此函数实现此功能。
    javascript作为弱对象语言,一个函数也可以作为一个对象使用。
    比如:
    function Test(){
    alert("hellow");
    this.NextStep=function(){
      alert("NextStep");
    }
    }
    我们可以这样调用 var myTest=new Test();myTest.NextStep();

    我们做暂停的时候可以吧一个函数分为两部分,暂停操作前的不变,把要在暂停后执行的代码放在this.NextStep中。
    为了控制暂停和继续,我们需要编写两个函数来分别实现暂停和继续功能。
    暂停函数如下:
    */
    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);
    }

    /*
    该函数把要暂停的函数放到数组window.eventList里,同时通过setTimeout来调用继续函数。
    继续函数如下:
    */
    function GoOn(ind){
        var obj=window.eventList[ind];
        window.eventList[ind]=null;
        if (obj.NextStep) obj.NextStep();
        else obj();
    }
    /*
    该函数调用被暂停的函数的NextStep方法,如果没有这个方法则重新调用该函数。
    函数编写完毕,我们可以作如下册是:
    */
    function Test(){
    alert("hellow");
    Pause(this,1000);//调用暂停函数 
    this.NextStep=function(){
      alert("NextStep");
    }
    }
    </script>

    ----------------------------------------------------------------------------------------------------

    以下脚本完成类似功能:

    function  checkuserinfo()
      {
          window.setTimeout(checkuses,5000);
          function  checkuses(){
             alert("5秒后我运行");
         }

    ==============================================================================

    方法二:使用函数闭包来实现

    ===============================================================================

    <input type="button" value="继续" onclick='st();'/>
    <script>
    /*函数*/
    function test(x)
    {
        alert(x++);
        alert(x++);
        return function()
        {
            alert(x++);
            alert(x++);
        }
    }
    var st = test(10);
    </script>

    这是基本形式,最多是多层嵌套问题。

    ===============================================================================

    方法三:正则替换

    ===============================================================================

    <html>
    <body>

    <button onclick="cont()">continue</button>
    </body>
    <script>

    function aa()
    {
        alert("1");
        pause();
        alert("2");
    }
    aa();

    var cont ;

    function pause()
    {
        window.onerror = function(){return true;};

        var re = /pause(\s|.)*}/;
        var s = pause.caller.toString();
        var r = s.match(re);
        r = r[0];
        var s2 = r.replace(/pause\(\);|}/g,"");

        cont = function() {eval(s2);window.onerror=null;};
        throw new Error();   
    }

    </script>
    </html>

    这样不能保存程序暂停时的当前状态...
    之所以用闭包是为了保存“调用堆栈”,包括所有全局和局部变量在暂停时的当前值等
    用闭包配合正则替换的原理...实现Pause()就比较完美了^^

    ================================================================================

    方法四:闭包+正则替换,有待实现