# setInterval에 클로져 개념 사용하기

{% embed url="<https://kyounghwan01.github.io/blog/JS/JSbasic/intervalFunction/>" %}
너무 깔끔한 정리
{% endembed %}

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval>" %}
MDN 공식 문서
{% endembed %}

기본적인 사용법은 위 사이트에서 확인하자.

## 상황

`setInterval` 이 특정 값의 조건에 맞으면 멈추게 하고 싶다. 위의 첫번째 링크를 확인해보면 아래와 같은 코드가 있다.

```javascript
var interval = setInterval(function() {
  console.log("Interval");
}, 1000);

clearInterval(interval);
```

위의 코드와 클로저의 개념을 사용해서 특정 조건을 추가해서 문제를 해결할 수 있을거 같다.

## 해결

```javascript
function setCountDownAsInterval() {
  let remain_time = 9;
  let myInterval = 0;

  return myInterval = setInterval(function () {
    if (remain_time === 0) {
      clearInterval(myInterval);
      myInterval = 0
      gameStop('time-out');
    }
    countDown(remain_time--)
  }, 1000);
}
```

여기서 나의 조건은 변수 `remain_time` 이 0 이 되면 `clearInterval` 을 해야하는 것. 위에 있던 **기존의 코드 한번 더 함수로 감싸서 해결했다.**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://simian114.gitbook.io/blog/undefined/javascript/setinterval.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
