1

part of templates/index.html

<script type="text/javascript">
var seconds = 0;
var el = document.getElementById('seconds-counter');

function incrementSeconds() {
    seconds += 1;
    el.innerText = "Online for " + seconds + " seconds.";
}

window.onload = function(){
alert("Bot Online");
setInterval(incrementSeconds, 1000);
}
</script>
<body>
<div id='seconds-counter'>0</div>
</body>

keep_alive.py

from flask import Flask, render_template
from threading import Thread

app = Flask('')

@app.route('/')
def main():
    return render_template('index.html')

def run():
    app.run(host="0.0.0.0", port=8080)
    
def keep_alive():
    server = Thread(target=run)
    server.start()

the window alerts but doesn't start the counter.
for context, this is a discord bot being hosted on replit.
any reason why this doesn't work?

davidism
  • 121,510
  • 29
  • 395
  • 339
L8R
  • 401
  • 5
  • 21
  • 1
    HTML is parsed top to bottom. When you define "el", it hasn't been drawn on the page yet, so "el" is null. Try putting your script tag after the div. – James Sep 28 '22 at 14:47
  • @James Worked. Post this as an answer? – L8R Sep 28 '22 at 15:53
  • 1
    There are a lot of complete answers available in the dupe link. ^^ Initially it wasn't clear that was the problem you were having. – James Sep 28 '22 at 15:57
  • I thought this issue was because of Flask because I have never encountered this error before. Thanks anyways. – L8R Sep 28 '22 at 16:23

0 Answers0