how to delay function execution in javascript?


  •   I'm writing a tic-tac-toe game to play against computer. So far the game is working fine. The problem is, computer acts too fast. When I play this game, it looks like the computer is taking it's turn just when user clicks a square. So how do I delay that function call when it's computer's turn?

      Can we call a function after some delay in javascript?

    Friday, June 14, 2013 7:29 PM


  • You could either call settimeout or WinJS.Promise.timeout which is a wrapper for settimeout.


    function doitlater()
        // call here whatever you like
        // call settimeout aggain so the function gets 
        // called another time
        setTimeout(doitlater, 1000);
    // set the initial timeout (1 sec)
    setTimeout(doitlater, 1000);

    Friday, June 14, 2013 9:01 PM