Underscore.js的Defer(或setTimeout 1)无法正常工作

skay-

在大型JavaScript应用程序中,我有一个类似的情况:

var $box = $('#box');
var expensiveOperation = function () {
    for (var i = 0; i < 10000; i++) {
        for (var j = 0; j < 4500; j++) {
            Math.random();
        }
    }
};

$('#show').click(function () {
    $box.show();
    expensiveOperation();
});

$('#showDefer').click(function () {
    $box.show();
    _.defer(expensiveOperation);
});

$('#hide').click(function () {
    $box.hide();
    expensiveOperation();
});
$('#hideDefer').click(function () {
    $box.hide();
    _.defer(expensiveOperation);
});
#box {
    background-color:red;
    width:100px;
    height:100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="box"></div>
<button id="show">show</button>
<button id="showDefer">show defer</button>
<button id="hide">hide</button>
<button id="hideDefer">hide defer</button>

jsFiddle链接,以防万一:http : //jsfiddle.net/oymaterz/5/

我想隐藏或显示DOM元素并执行昂贵的操作。出于性能原因,我要始终确保首先执行显示/隐藏(即在执行堆栈的顶部)。我提供的示例(使用下划线的延后)及其在最新版本的Chrome中的运行情况都很好地证明了这一点。另外,上面的示例不适用于IE11。隐藏/显示推迟了它的速度。

但是,当我在应用程序中执行相同操作时,它只能间歇性地工作,奇怪的是IE11似乎始终可以正常工作。

关于我为什么要得到这种行为的任何想法?

skay-

我找到了解决问题的办法。尽管预计(.setTimeout 1的)_.defer应该可以工作……在我的情况下,它并不是很一致。所以我最终使用了requestAnimationFrame方法。这是更新的版本:

var expensiveOperation = function () {
    for (var i = 0; i < 10000; i++) {
        for (var j = 0; j < 4500; j++) {
            Math.random();
        }
    }
};


var $box1 = $('#box-1');


$('#show-1').click(function () {
    $box1.show();
    expensiveOperation();
});

$('#showDefer').click(function () {
    $box1.show();
    _.defer(expensiveOperation);
});

$('#hide-1').click(function () {
    $box1.hide();
    expensiveOperation();
});

$('#hideDefer').click(function () {
    $box1.hide();
    _.defer(expensiveOperation);
});

var $box2 = $('#box-2');

$('#show-2').click(function () {
    $box2.show();
    expensiveOperation();
});

$('#showRequestAnimationFrame').click(function () {
    var flag = true;
    requestAnimationFrame(function showAnimFrame(){
        if(flag) {
            $box2.show();
            flag = false;
            requestAnimationFrame(showAnimFrame);
        } else expensiveOperation();
    });
});

$('#hide-2').click(function () {
    $box2.hide();
    expensiveOperation();
});

$('#hideRequestAnimationFrame').click(function () {
     var flag = true;
    requestAnimationFrame(function showAnimFrame(){
        if(flag) {
            $box2.hide();
            flag = false;
            requestAnimationFrame(showAnimFrame);
        } else expensiveOperation();
    });
});
#box-1 {
    background-color:red;
    width:100px;
    height:100px;
}

#box-2 {
    background-color:blue;
    width:100px;
    height:100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="box-1"></div>
<button id="show-1">show</button>
<button id="showDefer">show defer</button>
<button id="hide-1">hide</button>
<button id="hideDefer">hide defer</button>

<p></p>

<div id="box-2"></div>
<button id="show-2">show</button>
<button id="showRequestAnimationFrame">show requestAnimationFrame</button>
<button id="hide-2">hide</button>
<button id="hideRequestAnimationFrame">hide requestAnimationFrame</button>

jsFiddle链接,以防万一:http : //jsfiddle.net/oymaterz/6/

值得一提的是,IE8不支持requestAnimationFrame如果您需要支持,我建议您使用Paul Irish出色的GIST,以实现跨浏览器的兼容性。

我希望这会对与我类似的情况有所帮助。现在,对我来说,它显然可以正常工作,并且可以挽救生命。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

underscore.js _.defer的真实示例

来自分类Dev

underscore.js .keys和.omit无法按预期工作

来自分类Dev

以下Underscore.js代码如何工作?

来自分类Dev

以下Underscore.js代码如何工作?

来自分类Dev

jQuery settimeout()无法正常工作

来自分类Dev

角度 setTimeOut() 无法正常工作

来自分类Dev

JS无法正常工作

来自分类Dev

Underscore.js中的_()函数(对象包装器)如何工作?

来自分类Dev

underscore.js无法在html中使用<和>

来自分类Dev

噩梦JS无法正常工作

来自分类Dev

JS Slice()无法正常工作

来自分类Dev

JS淡入无法正常工作

来自分类Dev

无法使JS函数正常工作

来自分类Dev

JS Slice()无法正常工作

来自分类Dev

Underscore.js核心

来自分类Dev

Chrome扩展程序setTimeout无法正常工作

来自分类Dev

setTimeinterval中的jQuery setTimeout无法正常工作

来自分类Dev

在循环中调用setTimeout无法正常工作

来自分类Dev

Cordova / PhoneGap:setInterval()/ setTimeout()无法正常工作

来自分类Dev

JavaScript:setTimeout(setInterval)无法正常工作

来自分类Dev

jQuery setTimeout在循环中无法正常工作

来自分类Dev

Vue.js v1 无法与 Ajax 加载的 html 文件正常工作

来自分类Dev

如何安装underscore.js?

来自分类Dev

Underscore.js嵌套分组

来自分类Dev

Underscore.js反跳

来自分类Dev

Underscore.js如何计算

来自分类Dev

Underscore.js模板编译

来自分类Dev

ember.js clearInterval无法正常工作

来自分类Dev

Angular Js使Json无法正常工作