I have javascript that counts a timer lets say from 10 seconds.
That part works just fine!But the idea is that it show google captcha after theese 10 seconds but it just wont happens !
here is my javascript:
<script>
var seconds_left = "<?php echo $row['time']; ?>";
var interval = setInterval(function() {
document.getElementById('timer_div').innerHTML = --seconds_left;
if (seconds_left <= 0)
{
var content = "<div class='g-recaptcha' data-sitekey='keyhere' style='transform:scale(0.77);-webkit-transform:scale(0.77);transform-origin:0 0;-webkit-transform-origin:0 0;'></div>";
document.getElementById('timer_div').innerHTML = content;
clearInterval(interval);
}
}, 1000);
</script>
<div id="timer_div"></div>
What are you expecting that to do? Looking at the rest of the code, I’m assuming you’re running it in your browser, which has no understanding of PHP (even if it wasn’t just a string you’re setting seconds_list to). Have you checked what error messages you’re getting in the browser console?
I set up a Codepen just to test it out, and sure enough, it does count down without a problem, once you hard code a value for seconds_left. The thing I can’t see though, is anywhere for the captcha to come from (file ref?). At the moment, all the code does is output the value of the timer. Is there some more code we should be seeing (PHP?)?
function gRecaptchaReady () {
var secondsLeft = 10
var timerDiv = document.getElementById('timer_div')
var interval = setInterval(function() {
timerDiv.innerHTML = --secondsLeft;
if (secondsLeft <= 0) {
grecaptcha.render(
timerDiv,
{ sitekey: 'keyhere' }
)
clearInterval(interval)
}
}, 1000)
}
BTW, it’s really not good practice to generate JS code with PHP… jumbling languages like that quickly gets hard to read, maintain and debug. For example, if for some reason $row['time'] happens to be an empty string, you’ll get a syntax error and your script breaks. A more reliable way to pass that value would be as a data-* attribute in an associated HTML element, like
Ah yes, the captcha will get appended to the container element, not replace its content – just set timerDiv.innerHTML = '' before calling the .render() method.