簡體   English   中英

如何為每個自定義控件對象使用setInterval函數?

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

我創建了一個自定義控件,並在sapui5中創建了兩個對象。 在onAfterRendering函數的customControl.js中,我編寫了setInterval代碼,該代碼將定期更新自定義控件的value屬性。 我在視圖中創建自定義控件:

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

這是我的控制權:

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

但是似乎此方法有效時,它會更新具有相同值的所有自定義控件對象。 因此,當我將第一個控件的value屬性更新為52並將第二個控件的value屬性更新為72時。但是我只能看到兩個控件的72值。

我還嘗試在如下的onAftering方法中使用sap.ui.core.IntervalTrigger方法:

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

但這是行不通的,我上次嘗試使用閉包,但它再也行不通了。

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

@melomg,您應該發布更多詳細信息,包括自定義控件和updateControl函數的實現。 無論如何,我只能猜測出了什么問題...這是一個運行良好的示例(請參見下面的代碼)。 請檢查代碼中的注釋以獲取更多信息,尤其是由於您可能遇到的重新渲染循環問題(我只能猜測這一點)。

 <!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