简体   繁体   English

避免使用ko.compute主动聆听

[英]Knockout Avoid ko.computed active listening

how can i avoid height [ ko.computed] repeatedly run when i put a mouse over ? 当我将鼠标放在上方时,如何避免高度重复运行? [ mouseAreHoverFotoBanner ] [mouseAreHoverFotoBanner]

When a read something from ui, all ko.computed are automatically executed... 从ui读取内容时,所有ko.computed都会自动执行...

Code sample [ js ]: 代码示例[js]:

var ui = function () { var ui = function(){

            var fotoBanner = {
                scale: scale,
                scaleIn: function () { scale(0.9); },
                scaleOut: function () { scale(scale(0)); },
                mouseAreHoverFotoBanner: mouseAreHoverFotoBanner,
                enableHoverFotoBanner: function () { ( (mouseAreHoverFotoBanner()) ? "" :mouseAreHoverFotoBanner(true)); },
                disabelHoverFotoBanner: function () { mouseAreHoverFotoBanner(false); },

                url: ko.observable("270829_184226781631642_1559736_n.jpg"),
                height: ko.computed(function () {
                    toastr.info((this.wd.height() * scale()) + "px");//debug
                    return (this.wd.height() * scale()) + "px";
                }, this)
            };

            return { fotoBanner: fotoBanner };
        };

A computed will always automatically update, because it is a Knockout observable . 计算值将始终自动更新,因为它是可观察到淘汰赛 To maintain full control of updating: you may use a normal JavaScript variable, calculated from the Knockout observables wd.height() and scale() . 要保持对更新的完全控制:您可以使用普通的JavaScript变量,该变量是根据Knockout可观察对象wd.height()scale()计算得出的。 As a normal JavaScript variable, your new variable (say, " height ") can be updated only at the times you wish for it to update. 作为普通的JavaScript变量,新变量(例如“ height ”)只能在您希望对其进行更新的时间进行更新。

  • For example: 例如:

    var height = (this.wd.height() * scale()) + "px"; var height =(this.wd.height()* scale())+“ px”; var updateHeight = function () { var updateHeight = function(){

    } }

Ways to update: 更新方式:

  • Write a setTimeout() to update it after an amount of time you choose. 编写setTimeout()以在您选择的时间后对其进行更新。 This can be put in a while loop for repeated updating at an interval you choose. 可以将其放入while循环中,以便在您选择的时间间隔内进行重复更新。

  • Write an update function (say, updateHeight() ) and call it every time height() or scale() changes. 编写一个更新函数(例如updateHeight() ),并在每次height()或scale()变化时调用它。 This can be done by placing a subscribe() on height() and scale() : 可以通过在height()scale()上放置一个subscribe()来实现:

     wd.height.subscribe(function(newValue) { updateHeight(); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM