如何使用obj名称中的某些关键字对多个对象进行函数迭代?

时间:2019-06-21 19:12:28

标签: javascript

如何迭代对象名称中具有“设备”的对象(即:Device_0,Device_1,Device_2等)对每1/4秒发现的每个设备对象执行“ DeviceMonitor”更新功能。

我知道这对大家来说都是一流的东西,但是对于对象名称来说,只有在对象名称具有特定关键字并且每四分之一秒才执行一次,以免程序运行缓慢的情况下,才不得不采取行动,这对我来说太复杂了成为新手。目前,我有设备对象_0到_10,但将来还会增加,下面是我想每四分之一秒运行一次的每种对象和功能代码的示例。

这是为了更有效地模拟电路。我有一个使用意大利面条代码的工作模型,但是超过一千行代码,如果我能够按照说明进行工作,那么上述方法将是一种更好的方法。

   Wire_1 = 118.64;
   Wire_2;
   Wire_4 = Wire_3; 
   Wire_6 = Wire_5;
   Wire_8 = Wire_7;
   Wire_9;
   Wire_10;
   Neutral_4 = Neutral_3;
   Neutral_2 = Neutral_1 = 1;

   var Device_0 = new Object();
   Device_0.InputWire = Wire_1;
   Wire_2 = Device_0.OutputWire; 
   Device_0.model = "ConnectorDevice";
   Device_0.name= 'Fuse 1';
   Device_0.OnOff = 1;  
   Device_0.MinVoltage = 0; 
   Device_0.FailedDevice = 0;
   Device_0.IntermittentDevice = 0;

   var Device_3 = new Object();
   Device_3.InputWire = Wire_9;
   Neutral_3 = Device_3.OutputWire; 
   Device_3.model = "OutputDevice";
   Device_3.name= 'RunningLamp';
   Device_3.OnOff = 0;  
   Device_3.MinVoltage = 60; 
   Device_3.FailedDevice = 0;
   Device_3.IntermittentDevice = 0;

   var Device_5 = new Object();
   Device_5.InputWire = Wire_5;
   Wire_8 = Device_5.OutputWire; 
   Device_5.model = "SwitchDevice";
   Device_5.name= 'RunSwitch';
   Device_5.OnOff = 0;  
   Device_5.MinVoltage = 0; 
   Device_5.FailedDevice = 0;
   Device_5.IntermittentDevice = 0;

   function DeviceMonitor(DeviceNumber) {

   if (DeviceNumber.model === "OutputDevice") {
        if (DeviceNumber.InputWire > DeviceNumber.MinVoltage && !DeviceNumber.FailedDevice && !DeviceNumber.IntermittentDevice) {
            DeviceNumber.OnOff = 1;
        } else {
            DeviceNumber.OnOff = 0;
        }

    }

    if (DeviceNumber.model === "SwitchDevice") {
        if (DeviceNumber.FailedDevice || DeviceNumber.IntermittentDevice){
            DeviceNumber.OutputWire = 0;
        } else if (DeviceNumber.OnOff){
        DeviceNumber.OutputWire = DeviceNumber.InputWire;
        }

    }

    if (DeviceNumber.model === "ConnectorDevice") {
        if (DeviceNumber.FailedDevice || DeviceNumber.IntermittentDevice){
            DeviceNumber.OutputWire = 0;
        } else {
        DeviceNumber.OutputWire = DeviceNumber.InputWire;
        }
    }

    if (DeviceNumber.model === "Transformer") {

    }

}

理想地,为减少处理时间,如果仅在监视对象的任何属性发生更改时才运行更新功能,则效果会更好。但这将更加复杂。因此,每隔1.4秒就要做一次。

0 个答案:

没有答案