简体   繁体   English

是否可以使用javascript实时检查window.variable对象

[英]is it possible to check a window.variable object in realtime with javascript

Anyone know a way to control a variable in JavaScript in real time? 有人知道一种实时控制JavaScript变量的方法吗?

I'm implementing a code that in the face of automatic requests (signatures via pad) at some point when it arrives at the last signature must bring up a button (previously in hidden). 我正在实现一个代码,面对自动请求(通过pad签名),当它到达最后一个签名时,必须调出一个按钮(之前隐藏)。 The code of activate/disactivate is follow: 激活/取消激活的代码如下:

function checkButton() {
    var test = ClientInformation.WorkstepFinishCurrentlyAllowed;
    if (test == false) {
        $("#btnCustomFinish").addClass("hidden");
    } else {
        $("#btnCustomFinish").removeClass("hidden");
    }
}

I tried a solution using a setInterval () function that runs a check every few seconds but I do not find it very efficient. 我尝试使用setInterval()函数的解决方案,该函数每隔几秒运行一次检查但我发现效率不高。

setInterval(function() { 
    checkButton(); 
}, 500);

Does anyone know how to implement it through events, so that he is performing? 有没有人知道如何通过事件来实现它,以便他正在执行?

If necessary, use some code that automatically checks the variation in the value of the variables? 如有必要,使用一些自动检查变量值变化的代码?

Regards 问候

One option would be to use a Proxy for ClientInformation instead, so that assignments to the WorkstepFinishCurrentlyAllowed property of ClientInformation result in toggling the class on the button: 一种选择是使用Proxy进行ClientInformation代替,从而使分配到WorkstepFinishCurrentlyAllowed财产ClientInformation结果切换按钮类:

 const $btn = $("#btnCustomFinish"); const ClientInformation = new Proxy({}, { get(obj, prop) { return obj[prop]; }, set(obj, prop, val) { obj[prop] = val; if (prop !== 'WorkstepFinishCurrentlyAllowed') return; if (val) { $btn.removeClass("hidden"); } else { $btn.addClass("hidden"); } } }); console.log('start'); setTimeout(() => ClientInformation.WorkstepFinishCurrentlyAllowed = true, 500); setTimeout(() => ClientInformation.WorkstepFinishCurrentlyAllowed = false, 1000); setTimeout(() => ClientInformation.WorkstepFinishCurrentlyAllowed = true, 1500); setTimeout(() => ClientInformation.WorkstepFinishCurrentlyAllowed = false, 2000); 
 .hidden { display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="btnCustomFinish" class="hidden">button</div> 

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

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