tianyun 1 anno fa
parent
commit
290b2ca6af
1 ha cambiato i file con 26 aggiunte e 0 eliminazioni
  1. 26 0
      simple-demo/js/util.js

+ 26 - 0
simple-demo/js/util.js

@@ -448,4 +448,30 @@ function getTimeStamp() {
     const targetTimeStamp = new Date('2024-01-01').getTime();
     const difference = currentTimeStamp - targetTimeStamp;
     return difference;
+}
+
+
+/** 用于监控值的变化并执行相应的操作
+ * monitorAndAct(generateRandomValue, logValueChange);
+ *
+ * @param func1 入参:无 出参:值 generateRandomValue()
+ * @param func2 入参:旧值、新值 出参:无  logValueChange(oldValue, newValue)
+ */
+function monitorAndAct(func1, func2) {
+    let oldValue = undefined;
+
+    function checkValue() {
+        const newValue = func1();
+        if (newValue !== oldValue) {
+            // 值已改变,执行func2并传入旧值和新值
+            func2(oldValue, newValue);
+            // 更新旧值为新值
+            oldValue = newValue;
+        }
+        // 持续检查,这里假设每隔1秒检查一次
+        setTimeout(checkValue, 1000);
+    }
+
+    // 启动检查过程
+    checkValue();
 }