0

I have this script that you can see page changes in real time. So everything works great but for some reason when I try to copy the text the text become unhighlighted a second later.

I have to try to copy the text really fast before the setinterval updates the page again for new changes so my question is, is there a way were I can see constant changes on x.php

at the same time be able to copy text in any pace with out the interruption of the set interval updating the page constantly for new page changes?

Here is my code

index.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
function changes(){
$('.x').load('x.php');
}

setInterval(function(){changes()},1000);

});
</script>
<body onload='changes()'>
<div class='x'></div>
</body>

x.php

<p>Random text</p>
braaterAfrikaaner
  • 1,072
  • 10
  • 20
  • You want to copy text from an input or a

    or

    ?
    – Mojo Allmighty Jan 28 '18 at 00:10
  • index.php is outputting x.php content I made the example simple like this for simple understanding on what i'm trying to do. The actual code is the same but with alot more elements I just wanted to make this simple to people so you guys know what i'm trying to do. –  Jan 28 '18 at 00:14
  • Okay i understand that but the solution's complexity varies upon target. For example, for an input you will need to `clearInterval` upon focusing that input. For other elements the solution is the same but the trigger for focusing is a bit more complex. – Mojo Allmighty Jan 28 '18 at 00:17
  • So it's not possible with setInterval? It's just the div keeps on refreshing which the p tag is in that div that's why I can't copy the text. So clear Interval will allow me to copy the text and at the same time see new changes of that ajax div? Or do I need something else not related to setInterval? –  Jan 28 '18 at 00:38

1 Answers1

0

This is not an answer. It's just too long to post as comment.

What you need is to store the setInterval into a variable:

var handle = setInterval(function(){changes()},1000);

After this, whenever you want to stop the interval you will just call

clearInterval(handle);

The problem here is when you want to clear the interval. If you want to copy a text from an input, the solution is pretty simple. You clear the interval on focus and start it again on focusout or blur:

$('input').focus( function() {
  clearInterval(handle);
});

$('input').blur( function() {
  handle = setInterval(function(){changes()},1000);
});

However, the solution becomes more complex upon selecting a text from a p or div. You can see some solution on selecting text from div here : how to get selection inside a div using jquery/javascript . The solution pretty similar to the other one, you will clear interval on selecting and start it again on focus out. The harder part is to trigger when you select a text.

Mojo Allmighty
  • 793
  • 7
  • 18