首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在OpenLaszlo应用程序中,可以将while循环连接到鼠标悬停状态吗?

在OpenLaszlo应用程序中,可以将while循环连接到鼠标悬停状态吗?
EN

Stack Overflow用户
提问于 2012-09-19 02:33:27
回答 2查看 135关注 0票数 1

有没有可能做这样的事情

代码语言:javascript
复制
while (view.mouseover == true) {
    preform action
}

我希望只要鼠标停留在特定视图上,就可以重复执行操作。

(在laszlo用户邮件列表上询问)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-19 04:07:43

在我测试我的解决方案以确保它正常工作时,看起来您回答了自己的问题,但这里有一个在OpenLaszlo 4.9.0 SWF10和OpenLaszlo 4.9.0dhtml运行时工作的替代解决方案:

代码语言:javascript
复制
<canvas width="1000" height="665" debug="true">

  <view id="v" bgcolor="0xcccccc" width="200" height="200">

    <!--- @param boolean mouseisover: true when the mouse is over -->
    <attribute name="mouseisover" type="boolean" value="false" />

    <!--- @keywords private -->
    <!--- @param lz.Delegate dlgt_repeat: stores the lz.Delegate object --> 
    <attribute name="dlgt_repeat" type="expression" />

    <!--
    Called when the 'onmouseover' event occurs
    -->
    <handler name="onmouseover">

      // Step 1) unregister any existing delegate 
      // mark it for garbage collection 
      // and prevent its event from triggering:

      if (this['dlgt_repeat'])
        this.dlgt_repeat.unregisterAll(); 

      // Step 2) update this.mouseisover flag:

      if (!this.mouseisover)
        this.setAttribute('mouseisover', true);

      // Step 3) create an event Delegate and call it 
      // on the next application idle event:

      var objDlgt = new lz.Delegate(this, 'doSomething');       
      this.setAttribute('dlgt_repeat', objDlgt);

      lz.Idle.callOnIdle(this.dlgt_repeat);

    </handler>

    <!--
    Called when the 'onmouseout' event occurs
    --> 
    <handler name="onmouseout">

      // Step 1) unregister any existing delegate 
      // mark it for garbage collection 
      // and prevent its event from triggering: 

      if (this['dlgt_repeat'])
        this.dlgt_repeat.unregisterAll();   

      // Step 2) Update this.mouseisover flag:

      if (this.mouseisover)
        this.setAttribute('mouseisover', false);    

    </handler>

    <!--- @keywords private -->
    <!--- 
    Called on application idle event by lz.Idle repeatedly 
    when the mouse is down.

    @param ??? objDummy: required for SWF9+ run-times for methods 
    called by delegates due to AS3 (ActionScript3 compiler 
    requirements). Just set default to null to make compiler 
    happy and ignore...
    -->
    <method name="doSomething" args="objDummy=null">
    <![CDATA[

      // Note: CDATA allows '&&' to be used in script below, 
      // alternatively omit CDATA and use '&amp;&amp;' instead
      // of '&&'

      // Step 1) Execute your code you want to run here:  

      if ($debug) Debug.debug('Do something...');

      // Step 2): If mouse is still over and the event 
      // delegate exists then schedule the event to be 
      // executed upon the next application idle state:

     if (this.mouseisover && this['dlgt_repeat'] != null)
       lz.Idle.callOnIdle(this.dlgt_repeat);

   ]]>
   </method>

    <text text="Move mouse over" />

  </view>

</canvas>
票数 1
EN

Stack Overflow用户

发布于 2012-09-19 03:12:20

因为ActionScript和JavaScript都是单线程的,所以不可能在每次循环迭代之间有一个暂停的while循环。在SWF10/11运行时中,需要确保每个方法或函数中的代码都可以在应用程序的一个帧内执行(持续时间取决于SWF剪辑的帧率)。

作为一种解决方法,您可以使用计时器,下面是一个示例:

代码语言:javascript
复制
<canvas debug="true">

    <class name="mouseoverview" extends="view">        <attribute name="timer" type="object" value="null" />
        <!-- lz.Delegate instance used by the timer -->
        <attribute name="timerdel" type="object" value="null" />
        <attribute name="timeractive" type="boolean" value="false" />
        <!-- milliseconds to pause before each call to doWhileMouseover method -->
        <attribute name="tick" type="number" value="500" />
        <handler name="onmouseover">
            Debug.info('mouseover');
            if (this.timeractive == false) {
                this.setAttribute('timeractive', true);
                this.timerdel = new lz.Delegate( this, "timerCallback" );
                this.timer = lz.Timer.addTimer( this.timerdel, this.tick );
                // When the timer is activated, do one call to the method
                // containing the loop logic. The following calls will be
                // handled by the timer and delegate.
                this.doWhileMouseover();
            }
        </handler>
        <handler name="onmouseout">
            Debug.info('mouseout');
            if (this.timeractive) {
                this.setAttribute('timeractive', false);
                lz.Timer.removeTimer(this.timerdel);
            }
        </handler>
        <method name="timerCallback" args="millis">
            if (this.timeractive) {
                lz.Timer.resetTimer(this.timerdel, this.tick);
                this.doWhileMouseover();
            }
        </method>
        <method name="doWhileMouseover">
            Debug.info("This is your virtual while loop for mouseover");
        </method>
    </class>

    <mouseoverview x="100"
          y="100"
          width="400"
          height="400"
          bgcolor="#33aaff"
          tick="250">

    </mouseoverview>

</canvas>

当鼠标悬停时,使用timerdel (lz.Delegate的一个实例)启动计时器。然后直接调用doWhileMouseover()方法一次,然后重复使用计时器,只要没有发生onmouseout事件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12482915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档