Executing a function


(David Waller) #1

Probably a silly question.
Creating a noughts and crosses game against the computer.
When user clicks in one of the nine squares it calls a function to check not already used, enter the X character and check if there is a winning line.
It then calls another function that carries out the computer’s turn.
The problem is that the user’s X is not written until after the second function has been executed and so the X and O appear together.
Is there a way to force the inner HTML for the X to be written before the second function is called?
Thanks.


(Hans-Gerd Claßen) #2

I’d suggest posting your file for anyone being able to help you …


(Loves Hype) #3

In general you can delay a function call with setTimeout
https://www.w3schools.com/jsref/met_win_settimeout.asp

update: setTimeout instead of setInterval


#4

You could also use the API

hypeDocument.functions().theFunctionToCall(hypeDocument, element, event)

in the first function after your checks.


(David Waller) #5

Thanks. I have tried your suggestion but has the same problem.
The first function that calls the second one is not completed until the second one has run i.e. the user’s 'X’symbol is not written until the second function where the computer decides to go has been completed - therefore the X and O are written together.


#6

I think it’s the way you are calling/using your functions. It’s difficult to give you an exact solution as we don’t know how you’re calling your functions therefore we are just guessing as to what needs to be done. As Hans suggested … Can you share your doc?


(David Waller) #7

Thanks for your suggestions. I have solved the problem in a couple of ways:

  1. Calling the second function using setTimeout. It now completes the output from the calling function before calling the second one.
  2. Using the Main Timeline. The first function continues the paused timeline to an event that runs the second function and then restarts the timeline.
    Once again, thanks for your help.

(David Waller) #8

Thanks. This works really well.