zoukankan      html  css  js  c++  java
  • 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);
     
  • 相关阅读:
    spark性能调优 数据倾斜 内存不足 oom解决办法
    python2的中文编码
    spark UDF函数
    spark cache table
    spark 创建稀疏向量和矩阵
    mysql 分组排序
    给pyspark 设置新的环境
    CF662C Binary Table
    bzoj 4310 跳蚤
    3.29省选模拟赛 除法与取模 dp+组合计数
  • 原文地址:https://www.cnblogs.com/ly45/p/7598053.html
Copyright © 2011-2022 走看看