简体   繁体   English

如何为每个自定义控件对象使用setInterval函数?

[英]How can I use setInterval function for each custom control objects?

I created a custom control and created two objects of it in sapui5 . 我创建了一个自定义控件,并在sapui5中创建了两个对象。 in my customControl.js in onAfterRendering function I wrote a setInterval code which will update the value property of custom control periodically. 在onAfterRendering函数的customControl.js中,我编写了setInterval代码,该代码将定期更新自定义控件的value属性。 I create my custom controls in my view : 我在视图中创建自定义控件:

<controls:customControl
    id="customID1"
    value="50"/>
<controls:customControl
    id="customID2"
    value="70"/>

Here is my control : 这是我的控制权:

CustomControl.prototype.onAfterRendering = function()
{
    setInterval(this.updateControl(this), 500);
}

But it seems when this method works, it updates all custom control objects with same value. 但是似乎此方法有效时,它会更新具有相同值的所有自定义控件对象。 So when I update the value property of first control as 52 and the value property of second control as 72. But I only can see 72 value for both controls. 因此,当我将第一个控件的value属性更新为52并将第二个控件的value属性更新为72时。但是我只能看到两个控件的72值。

I also tried to use sap.ui.core.IntervalTrigger method in my onAftering method like below: 我还尝试在如下的onAftering方法中使用sap.ui.core.IntervalTrigger方法:

var iT = new sap.ui.core.IntervalTrigger(500);
iT.addListener(this.updateControl, this);

but this doesn't work and I last tried to use closure but it doesn't work again. 但这是行不通的,我上次尝试使用闭包,但它再也行不通了。

(function(self){   
    var iT = new sap.ui.core.IntervalTrigger(500);
    iT.addListener(self.updateGauge, self);
})(this);

@melomg, you should post some more details, including you custom control and the implementation of your updateControl function. @melomg,您应该发布更多详细信息,包括自定义控件和updateControl函数的实现。 I can only guess what's going wrong, anyway... Here is a working example (see code below) which runs just fine. 无论如何,我只能猜测出了什么问题...这是一个运行良好的示例(请参见下面的代码)。 Check the comments in the code for more information, especially because of the re-rendering loop issue that you might have (I can only guess this). 请检查代码中的注释以获取更多信息,尤其是由于您可能遇到的重新渲染循环问题(我只能猜测这一点)。

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>SAPUI5 single file template | nabisoft</title> <script src="https://openui5beta.hana.ondemand.com/resources/sap-ui-core.js" id="sap-ui-bootstrap" data-sap-ui-theme="sap_bluecrystal" data-sap-ui-libs="sap.m" data-sap-ui-bindingSyntax="complex" data-sap-ui-compatVersion="edge" data-sap-ui-preload="async"></script> <!-- XMLView --> <script id="myXmlView" type="ui5/xmlview"> <mvc:View xmlns="sap.m" xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" xmlns:nabisoft="nabisoft"> <!-- use our custom control, see below --> <nabisoft:CustomControl id="one" value="1" interval="500"/> <nabisoft:CustomControl id="two" value="90000" interval="1000"/> </mvc:View> </script> <script> sap.ui.getCore().attachInit(function () { "use strict"; //### Custom Control ### jQuery.sap.declare("nabisoft.CustomControl"); sap.ui.core.Control.extend("nabisoft.CustomControl", { metadata : { properties : { value : {type : "int"}, interval : {type : "int", default:500} }, aggregations : {}, associations: {}, events : {} }, init : function(){}, onAfterRendering: function (){ setInterval(this.updateControl.bind(this), this.getInterval()); }, updateControl : function () { var iOldValue = this.getValue(); var iNewValue = iOldValue + 1; // don't do this, because this will lead to a rerendering loop!!! //this.setValue( iNewValue ); //instead do this here: this.setProperty("value", iNewValue, true /*supress rerendering*/); this.$().find("span").text(iNewValue); }, renderer : { render : function(oRm, oControl) { oRm.write("<div"); oRm.writeControlData(oControl); oRm.addClass("nsCustomControl"); oRm.writeClasses(); oRm.write(">"); oRm.write("<div>" + oControl.getId() +" : <span>" + oControl.getValue() + "</span></div>"); oRm.write("</div>"); } } }); //### THE APP: place the XMLView somewhere into DOM ### sap.ui.xmlview({ viewContent : jQuery("#myXmlView").html() }).placeAt("content"); }); </script> </head> <body class="sapUiBody"> <div id="content"></div> </body> </html> 

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

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