使用javascript定期调用php脚本 [英] Calling a php script at regular intervals with javascript

查看:68
本文介绍了使用javascript定期调用php脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php和javascript定期在页面的一部分上生成输出。我是js的新手,所以请原谅任何迟钝的问题。

I'm working on generating output on a section of a page at regular intervals with php and javascript. I'm brand new to js, so please forgive any obtuse questions.

原始的PHP代码是;

The original php code is;

<?php

$data = file_get_contents("http://" . $phoneControlUsername .":" . $phoneControlPassword . "@" . $ip . "/CGI/Screenshot");
$filename = "tempFiles/phoneScreenshot$dev";
file_put_contents($filename, $data);
echo "<img src=" . $filename . " style='width:100%;'>"

?>

这曾经是我主页的一部分,但我把它移到了自己的文件screenGen .PHP。我的主页可以完全没问题地调用它;

This used to be part of my main page, but I moved it over to its own file called screenGen.php. My main page can call on it with no problem at all;

require('screenGen.php');

我需要每五秒生成一次这个特殊代码。因此,在阅读javascript和php如何一起使用时,我想出了;

I need this particular code to be generated every five seconds. So, in reading how javascript and php can be used together, I came up with;

<script>

window.setInterval(function() 
        {
        screenGen = '<?php require('screenGen.php'); ?>';
        document.write(screenGen);
        }, 5000);

</script>

当然,这不起作用。我无法找到关于如何做这样的事情的正确方向,因为对于这个主题是新手,我不知道如何寻找它。如何定期生成screenGen.php?

Of course, this does not work. I can't find the right directions on how to do something like this because, being new to the subject, I don't know how to look for it. How can I generate screenGen.php at regular intervals?

推荐答案

我找到的答案比发布的答案简单得多。

I found an answer that's simpler than the ones posted.

1)在服务器上安装jquery。如果您使用的是Debian,可以使用安装sudo apt-get install libjs-jquery

1) Install jquery on your server. If you're using Debian, you can install with sudo apt-get install libjs-jquery

2)In你的PHP;

2) In your PHP;

<div id="auto"></div>

<script language="javascript" type="text/javascript" src="/javascript/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready( function()
        {
        $('#auto').load('screenGen.php');
        refresh();
        });

function refresh()
        {
        setTimeout( function()
                {
                $('#auto').load('screenGen.php');
                refresh();
                }, 5000);
        }
</script>

这篇关于使用javascript定期调用php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆