2016-04-12 3 views
0

次のコマンドを使用してスナップショットを撮るjQueryカメラプラグインがあります。jqueryを実行する前に5からカウントダウン

<img id="camera_icon" src="images/icons/camera-icon2.png" onClick="take_snapshot()"> 

これは実行されるコードです。

<script language="JavaScript"> 
function take_snapshot() { 
    // take snapshot and get image data 
    Webcam.snap(function(data_uri) { 
     // display results in page 
     document.getElementById('results').innerHTML = 
      '<h2>Saved Snapshot</h2>' + 
      '<img src="'+data_uri+'"/>'; 

     Webcam.upload(data_uri, 'save_snapshot.php', function(code, text) { 
      // Upload complete! 
      // 'code' will be the HTTP response code from the server, e.g. 200 
      // 'text' will be the raw response content 
     }); 
    }); 
} 
</script> 

私がダウンして5から数えて、それが誰かがボタンをクリックすると、それは瞬時に写真を撮ってますが、今のところ0に達したときにスナップショットを取得するソリューションを探しています。ソリューションは気にする必要はありませんが、ユーザーが5から数えていることをユーザーに示す必要があります。

+4

[できるだけ簡単なJavaScriptを 'setInterval' – guradio

+0

https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval –

+0

可能な重複についての記事を読みますカウントダウンタイマー?](http://stackoverflow.com/questions/20618355/the-simplest-possible-javascript-countdown-timer) –

答えて

1

setTimeoutを参照すると、関数を呼び出したり、コードスニペットを実行したりすることができます。時間を設定します。

Webcam.snap関数をwindow.setTimeoutとラップすることができます。

function take_snapshot() { 
 

 
    var timer = document.getElementById('shutter'); 
 
    timer.setAttribute("disabled", "disabled"); 
 
    timer.innerHTML = 5; 
 

 
    var countdown = window.setInterval(function() { 
 
    var seconds = timer.innerHTML; 
 
    seconds = seconds - 1; 
 
    timer.innerHTML = seconds; 
 

 
    if (seconds == 0) { 
 
     timer.innerHTML = "Take Pic"; 
 
     timer.removeAttribute("disabled"); 
 
     clearInterval(countdown); 
 
    } 
 
    }, 1000); 
 

 
    window.setTimeout(function() { 
 

 
    // Run your code here 
 

 
    }, 5000); 
 
}
button { 
 
    width: 80px; 
 
    height: 80px; 
 
    border-radius: 40px; 
 
    font-size: 14px; 
 
}
<button onClick="take_snapshot()" id="shutter">Take Pic</button> 
 
<div id="timer"></div>

+0

ありがとうございます。実際にカウントダウンを組み込んで、私が正しい方向に向かうようにする方法を知っていますか?ありがとうございました。 –

+0

詳細は[ペン](http://codepen.io/brettdewoody/pen/vGdaQK)です。基本的な考え方は、 'setInterval'を使ってカウントダウンを作成し、カウントダウンをページに表示することです。私の例では、ボタンにカウントダウンを表示することを選択しました。 –

+0

絶対に素晴らしいです。どうもありがとうございました!!! –

関連する問題