If I have files called index.php
and fetch.php
index.php contains:
<script>
$(document).ready(function(){
setInterval(function(){
$('#fetch').load('fetch.php')
}, 1000);
});
</script>
<div id="fetch"></div>
And fetch.php contains:
$sql = "SELECT * FROM posts ORDER BY id DESC LIMIT 20";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '
<div>'.$row['post'].'</div><textarea id="comment" placeholder="Comment"></textarea><button id="btn">Comment</button>
';
}}
I have a problem here, Interval is reloading every 1s and whenever I click on text area it just get reloaded as well. Is there any way to stop interval inside index.php by clicking on text area from code.php and then by button setting it back...
I have been working on click on text area to store into db the word commenting and set it here as nothing:
setInterval(function(){
$('commenting').load('fetch.php')
}, 1000);
and after the button click set it back to
setInterval(function(){
$('#fetch').load('fetch.php')
}, 1000);
But it also needs reload whole index.php page to change $('#fetch') to $('commenting')
So I am wondering is there any way to stop interval from file that is included inside that interval?