I have a page that contains a div that refreshes every 3 seconds that looks like this:
<script>
$(document).ready(function(){
setInterval(function(){
$("#here").load(window.location.href + " #here" );
}, 3000);
});
</script>
<div id="here">
/* Div content, contains an html table*/
</div>
I want the refresh script to stop if a certain <td> in the HTML table is clicked. When the user clicks outside of the cell, I want the refresh script to continue running.
Is this at all possible?
2
Answers
start()
functionsetInterval
to start the counter, remember it’s returnedpid
end()
functionclearInterval
on thepid
to stop the countertr
‘s that will call thestop()
functiondocument
that will callstart()
if we’re not already counting (!pid
) and we didn’t click on a<tr>
or<th>
:![ 'TD', 'TH' ].includes(e.target.nodeName)
You can give a variable name to
setInterval
function and make that variable global. Then, set anonclick
event in your<td>
which will clear thesetInterval
. For example:Here, as name suggests,
clearInterval
function clears Interval which was gave it to.See more at W3Schools’ website.