Skip to main content
Known Participant
April 10, 2011
Question

Trying to increment counter after five seconds if there is text in a window

  • April 10, 2011
  • 2 replies
  • 823 views

I am trying to increment a counter by one digit if a window (called searchItem_txt) contains text for more than five seconds. The code below activates the counter after five seconds but it keeps on incrementing until the count has reached 63. Any suggestions for fixing this code would be much appreciated. Thanks.



var SearchItemTimer:Timer = new Timer(5000, 1);
SearchItemTimer.addEventListener(TimerEvent.TIMER, CountOneClearWindow);

function CountOneClearWindow(event:TimerEvent):void
{
    counter = counter + 1;
    customerCount_txt.text = String(counter);
    searchItem_txt.text = ("");
}


if (searchItem_txt.text != "")
{
    SearchItemTimer.start();
}

This topic has been closed for replies.

2 replies

Inspiring
April 10, 2011

I am still not sure what you are trying to do but try the following:

import flash.events.Event;
import flash.events.TextEvent;
import flash.text.TextField;
var counter:int = 0;
// listen to field change
searchItem_txt.addEventListener(Event.CHANGE, onInputChange);
// variable names should start with lower case
// because by conventions classes start with upper case
var searchItemTimer:Timer = new Timer(5000, 1);
searchItemTimer.addEventListener(TimerEvent.TIMER, CountOneClearWindow);

function CountOneClearWindow(e:TimerEvent):void
{
    counter++;
    customerCount_txt.text = String(counter);
    searchItem_txt.text = ("");
}

function onInputChange(e:Event):void {
     if (searchItem_txt.text != "") searchItemTimer.start();
}

Known Participant
April 10, 2011

Thanks very much for your suggestion. I just tried the code without any

attempt to adapt it to my program and I see some error messages. I need to

break away from this task to do my taxes but will return when I have some

time available.

Inspiring
April 10, 2011

Do you mean that timer shouldn't fire if text is empty and fire only if there is text? Also, what is 63?

Known Participant
April 10, 2011

Yes, the timer should not fire if the window containing text is empty. If there is text in the window, I want it to remain until five seconds have lapsed, then the timer should activate, the counter should increment from 1 to 2 or from 5 to 6 etc and the text in the window should be removed. The number 63 is the number that the present code causes the counter to increment to from the initial count of one.

Inspiring
April 10, 2011

So, I guess the code I posted accomplishes what you want.