将函数作为参数传递给jQuery模板

时间:2018-04-18 06:59:08

标签: javascript function parameters jquery-templates

我试图将函数作为参数发送到jQuery模板并显示函数返回的第一个元素。

在下面的示例中,数据变量是一个函数,我尝试追加返回的第一个元素。它不是将数据存储到变量中的解决方案,因为在很多情况下数据是数组而不是函数。

var template= ' {{each things}}{{if $index == 0}}<span>${this.Name}</span>{{/if}}{{/each}}';

$('.dropdownText').text(jq.tmpl(template, data));

1 个答案:

答案 0 :(得分:0)

我不确定我是否有问题但如果问题是数据可能是函数或数组你可能只需要这样做

public float waitTime = 0.0f;
public float fadeTime = 1.0f;
public float betweenFadesTime = 1.0f;

// Flag to determine whether or not the player may respond.
public bool canRespond = false;

// Flag to determine if the player has responded within the wait time.
public bool hasResponded = false;

public GameObject enemy;

void Start()
{ 
    StartCoroutine(EnemyFadeIn(fadeTime, betweenFadesTime, waitTime)); 
}

void Update() {
    if ((Input.GetKeyDown(KeyCode.S)) && (canRespond))
    { hasResponded = true; }
}

IEnumerator EnemyFadeIn(float timeToFade, float timeBetweenFades, float timeToWait) {
    Debug.Log("An Enemy Is Fading In");
    // Simulating Fade In Time.
    yield return new WaitForSeconds(timeToFade);
    Debug.Log("An Enemy Has Appeared");
    yield return ListenForInput(timeToFade, timeBetweenFades, timeToWait * 60f);
}

IEnumerator EnemyFadeOut(float timeToFade, float timeBetweenFades, float timeToWait) {
    Debug.Log("An Enemy Is Fading Away");
    // Simulating Fade Out Time.
    yield return new WaitForSeconds(timeToFade);
    Debug.Log("An Enemy Has Departed");
    // Simulating Time Between Fades.
    yield return new WaitForSeconds(timeBetweenFades);

    yield return EnemyFadeIn(timeToFade, timeBetweenFades, timeToWait);
}

// Responsible for reacting to the 'S' key input.
IEnumerator ListenForInput(float timeToFade, float timeBetweenFades, float timeToWait) {
    canRespond = true;
    Debug.Log("Press the 'S' Key to Destroy the Enemy!");
    float startTime = Time.time;

    // Check every 0.25 seconds to see if the S key was pressed.
    while (Time.time < (startTime + timeToWait)) {
        if (hasResponded) {
            Debug.Log("The 'S' Key was Pressed!");
            hasResponded = false;
            canRespond = false;
            yield return EnemyFadeOut(timeToFade, timeBetweenFades, timeToWait);
        }
        yield return new WaitForSeconds(0.25f);
    }