Programming WaitForSeconds

Just out of curiosity (I'm learning yield and unity scripting), would it be possible to implement WaitForSeconds in Javascript? How would you do it?

What about C#?

EDIT: Ok, it seems I didn't express myself correctly. My question is hypothetical, let's say you didn't have the WaitForSeconds class available and you needed to create it from scratch. How would you program a class that had the same behavior as the class provided by Unity?

In Javascript:

function Start () {
    yield MyWaitFunction (1.0);
    print ("1");
    yield MyWaitFunction (2.0);
    print ("2");
}

function MyWaitFunction (delay : float) {
    var timer = Time.time + delay;
    while (Time.time < timer) {
        yield;
    }
}

In C#:

using UnityEngine; 
using System.Collections; 

public class TimerTest : MonoBehaviour { 
    IEnumerator Start () {
        yield return StartCoroutine(MyWaitFunction (1.0f));
        print ("1");
        yield return StartCoroutine(MyWaitFunction (2.0f));
        print ("2");
    }

    IEnumerator MyWaitFunction (float delay) {
        float timer = Time.time + delay;
        while (Time.time < timer) {
            yield return null;
        }
    }
}

Alternately (Javascript):

function MyWaitFunction (delay : float) {
    var t = 0.0;
    while (t < delay) {
        t += Time.deltaTime;
        yield;
    }
}

javascript

function Start ()
{
Debug.Log ("start time");
yield WaitForSeconds(2);
Debug.Log ("two seconds later");
}

in C# you should add System.Collections to your namespaces. also you define a coroutine by returning an IEnumerator.

IEnumerator Start ()
{
print ("first");
yield return new WaitForSeconds(2f);
print ("2 seconds later");
}

2f or 2.5f means that the number is a float and not a double or int. you should return an instance of WaitForSecond in C#. see this pages for more info. http://unity3d.com/support/documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

just put this in the function where ever you need it to stop for a while:

yield WaitForSeconds(4); //4 being the number of seconds you want