• lockable JS function,解锁操作之前,不能重复操作


            (function () {
                var ed = [];
                window.Lockable = function (lockF, options) {
                    if (!arguments.length) {
                        var caller = arguments.callee.caller;
                        if (ed.indexOf(caller) === -1) {
                            ed.push(caller);
                            caller.unlock = function () {
                                ed.splice(ed.indexOf(caller), 1);
                            };
                            return;
                        }
                        return true;
                    }
                    var empty = function () { },
                        on = options && options.on || empty,
                        off = options && options.off || empty,
                        repeat = options && options.repeat || empty,
                        ing,
                        ri = 1;
                    if (typeof options === "function") {
                        off = options;
                    }
                    var f = function () {
                        "use strick"
                        if (ing) {
                            repeat(ri++);
                            return;
                        }
                        if (on()) {
                            ing = false;
                            return;
                        }
                        var r = lockF.apply(this, arguments);
                        if (typeof r === "undefined") {
                            ing = true;
                        } else {
                            ing = !!r;
                        }
                    };
                    f.unlock = function () {
                        ing = false;
                        off.apply(arguments);
                    };
                    return f;
                }
            })();

    使用方法:

    var functionName = Lockable(function(){
      //needs long time.
      functionName.unlock();
    });
    
    setInterval(functionName, 111);

    或者:

    function fn(){
        if(Lockable()) {
            return;
        }
        //needs long time
        fn.unlock();
    }
    setInterval(fn, 111);
     
  • 相关阅读:
    (spring-第15回【IoC基础篇】)容器事件
    javascript笔记2-引用类型
    php namespace use 命名空间
    mysql性能优化(二)
    mysql常用命令
    关于有效的性能调优的一些建议[转]
    apache htaccess
    php 换行 空格分割处理
    一些比较好的博文
    php & 引用
  • 原文地址:https://www.cnblogs.com/ly45/p/7598053.html
Copyright © 2020-2023  润新知