I am incrementing a value from my database evey 3.5 sec using javascript, but the value that i am incremeting is coming dynamically from the database, now i want to save the newly incremented value to the database as the increment keeps going, i don't know how to achieve this using django and javascript.
When i refresh the page, the value resets to the default value that i have in my database and doesn't update the value with the new increments
<span id="generated">{{total_points}}</span>
<!-- This is the js doing the increment function -->
<script>
var i = {{total_points}};
function increment() {
i++;
document.getElementById('generated').innerHTML = `₦` + Number(i).toLocaleString('en') + `.00`;
}
setInterval('increment()', 3140);
</script>
how do i save the new increments to the database? Do i need to provide my models.py?
The total points and the increment you can save in the javascript value until there is a submit of the form. Then send the result in the request to safe it to the database. If you refresh the page you indeed loose the increment. It might also be interesting for you to safe stuff in localstorage because if the user then refreshes the localstorage is still there, but then you must take localstorage if there is one, {% else ... you must take {{total_points}} to start with, but also with this: Updating stuff to the database requires you to build a form and do a request.