views:

39

answers:

3

I'm having 2 problems.

1 - I have a text area in which I am matching a string. If it matches then a timer will start. But that condition will likely be satisfied more than once, so I would like to give a condition that if timer is already running don't do anything, if(!timer.running) then start timer. But it still resets the timer every time.

2 - I have a chat window. For every user activity a sentence will be displayed to it. For each added sentence I have to perform some actions. So i have given conditions and actions to be performed for each sentence in a single function, but the problem is every time the previous already executed commands are also executed one more time. (for example above problem 1.)so once it matches the 1st string it should start search from 2nd line in the text area, i think this can do the trick. any help will be appreciated.

public function updateMessage(updateMsg:String) : void
{
  userActivities.text+=updateMsg+"\n";

  if(userActivities.text.indexOf("user connected",0)!=-1)
  { 
    userTimer=new Timer(delay);

    if(!userTimer.running)
    { 
      basetmr=getTimer();
      userTimer.addEventListener(TimerEvent.TIMER,chkUserActivities);
      userTimer.start();
    }
    else
    {
      //trace("timerCount.."+userTimer.currentCount);
    }
  } 
  else if(userActivities.text.indexOf("user changed the image",0)!=-1 )
  {
    userActivities.text+="Click ReleaseDetails button to release your details to visitor";
  }
  else if(userActivities.text.indexOf("user quit the session",0)!=-1)
  {
    userTimer.stop();
  }
}  
A: 

Your code is wrong. Here is some comments:

// this line creaets a new timer        
userTimer=new Timer(delay);

// no code here to start the timer running

// userTimer.running will always be false and this condition will always be true
if(!userTimer.running)

I bet if you change the first line of your method to something like this:

if(userTimer){    
 userTimer=new Timer(delay);
}

It would work because you will not be re-initializing the timer on ever method call.

www.Flextras.com
A: 

As it stands you are creating a new timer and then checking if its running, so I think part one would be improved by changing from

userTimer=new Timer(delay);

if(!userTimer.running)
{ 
  basetmr=getTimer(); //etc

To something like

if(!userTimer.running)
{ 
  userTimer=new Timer(delay);
  basetmr=getTimer(); //etc

For part 2 I'm not familiar enough with flex to be able to help, but instead of checking userActivities.text you want to check the latest subsection of it, such as just the last sentence.

PaulG
A: 

hi www.flextras.com, thanku for ur valuable and quick reply , it helped me.....thanks a lot.

bagi