문제

이 스크립트 수신 시간부터 www.time.is 에서#twd ID 를 표시할 때 보내기를 클릭하십 btn.

나는 두 가지 문제점:

1-만 먼저 받을 시간이 업데이트하지 않는 경우 클릭하에 다시 보내기

2 페이지를 새로 고침과 데이터를 잃어버

나는 세 가지 문제가 아래 코드:

<?php include 'simple_html_dom.php'; ?>
<!DOCTYPE html>
<html>

    <head>
        <title>Untitled 1</title>
        <script src="js/jquery.js"></script>
    </head>

    <body>

        <form action="" method="POST">
            <input id="url" name="url" type="text" value="http://www.time.is/">
            <input id="address" name="address" type="text" value="#twd">
            <input id="send" type="submit" value="get">
        </form>

        <ul id="times">

        </ul>

        <script type="text/javascript">
            $("#send").click(function(events) {
                events.preventDefault();

                var url = $("#url").val();
                var address = $("#address").val();
                var dataString = 'url=' + url + '&address=' + address;

                $.ajax({
                    type: "POST",
                    url: "read.php",
                    data: dataString,
                    success: function() {
                        var result = "<?php echo getme($url,$address);  ?>";
                        alert(result);
                        $('#times').append('<li></li>');
                    }
                });
                return true;
            });
        </script>

        <?php 

            function getme($url, $address) {
                $html = file_get_html($url);
                $code = $html->find($address, 0);
                return $code;
            }

            echo getme($url, $address);

        ?>
    </body>
</html>

모든 몸을 도울 수 있습니다.

모든 고맙습니다

도움이 되었습니까?

해결책

이것을 보십시오:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){

                function timer(){
                $.post( "process.php", function( data ) {
                    $('#times').append('<li>'+data+'</li>');
                });
                }

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


            });
        </script>
    </head>

    <body>
         <ul id="times"></ul>
    </body>
</html>

에 넣고 당신 process.php 파일:

<?php
    include 'simple_html_dom.php';
    $html = file_get_html('http://www.time.is/');

    foreach($html->find('#clock0') as $element){
        echo $element->plaintext;
    }
?>

이 테스트 결과는:

  • 11:15:43AM
  • 11:15:46AM
  • 11:15:51AM
  • 11:15:53 분
  • 11:15:53 분
  • 11:16:10
  • 11:15:52AM
  • 11:15:42AM
  • 11:16:09AM
  • 11:16:17AM
  • 11:16:12

참고:

1-는 것이 좋습을 변경 간격으로 1000 밀리초(1 초)입니다.

2-을 사용하는 것을 잊지 마십시오 clearInterval() 후에는 특정 반복합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top