简体   繁体   English

代码在 Javascript 中运行的时间/频率

[英]When/how often does code run in Javascript

I'm just trying different things to learn how javascript works, and I'm wondering why the following doesn't work.我只是在尝试不同的事情来学习 javascript 的工作原理,我想知道为什么以下内容不起作用。

I've got a function that toggles independent_color between "blue" and "red" on button click:我有一个功能,可以在单击按钮时在"blue""red"之间切换independent_color "blue"

$scope.independent_color = "blue";

$scope.toggle_independent_color = function() {
    if ($scope.independent_color == "blue") {
        $scope.independent_color = "red"
    }
    else {
        $scope.independent_color = "blue"
    }
}

Then, I have some conditionals that set dependent_color based on the value of independent_color :然后,我有一些条件根据independent_color的值设置dependent_color

if ($scope.independent_color == "blue") {
    $scope.dependent_color = "blue"
}

else {
    $scope.dependent_color = "red"
}

However, when I toggle independent_color , the value of dependent_color does not change.但是,当我切换independent_colordependent_color的值不会改变。

Here's a FIDDLE: https://jsfiddle.net/exr3tqm0/5/这是一个小提琴: https ://jsfiddle.net/exr3tqm0/5/

My question is not how to get this to work.我的问题不是如何让它发挥作用。 Rather, I'd like to know why it doesn't work.相反,我想知道为什么它不起作用。 When/how often does the code run?代码何时/多久运行一次?

It does not change because this code:它不会改变,因为此代码:

if ($scope.independent_color == "blue") {
    $scope.dependent_color = "blue"
}

else {
    $scope.dependent_color = "red"
}

lies directly in controller function, so it's executed only once, when controller is created (when you enter the view).直接在控制器函数中,所以它只执行一次,当控制器被创建时(当你进入视图时)。 Simply change your dependantColor in toggle_independent_color .简单地改变你的dependantColortoggle_independent_color You can also handle this dependency using angular's $scope.watch but it's not needed in your case.您也可以使用 angular 的$scope.watch处理此依赖$scope.watch但在您的情况下不需要它。

The code runs once.代码运行一次。 The function for toggle_independent_color runs as many times as you click (your fiddle shows it attached via ng-click). toggle_independent_color的函数在您单击时运行多次(您的小提琴显示它是通过 ng-click 附加的)。 All the other code runs once when your controller is instantiated.所有其他代码在您的控制器实例化时运行一次。 If you instantiate your controller multiple times (such as in having multiple of the same views) then it will run that many times, but each instantiated controller will have its own child scope.如果你多次实例化你的控制器(比如有多个相同的视图),那么它会运行很多次,但每个实例化的控制器都有自己的子作用域。

On a side note, to get it to work, you can try putting that dependent_color functionality into another function.附带说明一下,要使其工作,您可以尝试将该dependent_color功能放入另一个函数中。

var app = angular.module("myApp", []);

app.controller("myController", ["$scope", function($scope) {

$scope.independent_color = "blue";

$scope.toggle_independent_color = function() {
    if ($scope.independent_color == "blue") {
        $scope.independent_color = "red"
    }
    else {
        $scope.independent_color = "blue"
    }
    checkDependent(); // check the dependent color when the button is clicked
}

// this check needs to be run each time the color is changed
function checkDependent(){
  if ($scope.independent_color == "blue") {
      $scope.dependent_color = "blue"
  }
  else {
      $scope.dependent_color = "red"
  }
}

}]);

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

相关问题 AngularJS摘要循环多久运行一次? - How often does the AngularJS digest loop run? 事件循环在浏览器中多久运行一次? - How often does the event loop run in a browser? RegisterStartupScript的含义:会多久调用一次JavaScript代码? - Making sense of RegisterStartupScript: How often will the JavaScript code be called? JavaScript在函数中重新编译正则表达式文字的频率是多少? - How often does JavaScript recompile regex literals in functions? Angular 2+中* ngIf条件中的代码多久会发生一次火灾? - How often does code in an *ngIf condition in Angular 2+ fire? 将不需要经常运行的代码与 Javascript 中另一个循环中的主游戏循环分开是否更有效? - Is it more efficient to separate code that doesn't need to be run as often from the main game loop in another loop in Javascript? 在查看其他开发人员的javascript代码时,我经常看到JSON对象/ fn习惯于解析JSON? 这个物体从哪里来? - When looking at other dev's javascript code, i often see a JSON object/fn getting used to parse JSON? Where does this object come from? .scroll()多久会一次火? - How often does .scroll() fire? Javascript-全局导入如何使代码运行更快? - Javascript - How does global import make the code run faster? 加载网页时如何自动运行自定义 JavaScript 代码? - How to automatically run custom JavaScript code when loading a webpage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM