본문 바로가기
Coding/JavaScript

[JavaScript & jQuery] 움직임, 입력이 없을 시 자동 새로고침&경로이동 하는 방법 !!

by 포스트it 2023. 6. 17.
728x90
반응형

 

[JavaScript & jQuery] 움직임, 입력이 없을 시 자동 새로고침&경로이동 하는 방법 !!

특정 행동이 없이 가만히 있을 때 새로고침이나 경로 이동하게 하는 방법 입니다 :)

 

예제코드
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<input type="text" id="input" value="" placeholder="글을 입력해주세요.">
</body>

</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">

    let idleTime = 0;
    $(document).ready(function () {
        // 1분마다 일정시간 움직임이 있으면 초기화
        let idleInterval = setInterval(timerIncrement, 60000); 
        $(this).mousemove(function (e) { idleTime = 0; });
        $(this).keypress(function (e) { idleTime = 0; });
    });
    
    function timerIncrement() {
        idleTime = idleTime + 1;
        // 20분이상 움직임이 없으면 새로고침 & 경로이동
        if (idleTime > 19) { 
            window.location.reload();
            // window.location.href = "https://www.naver.com/";
        }
    }

</script>

결과값

지속적인 새로고침으로 결과값 제거하였습니다.

728x90
반응형

댓글