【问题标题】:Save every x minutes with html2canvas使用 html2canvas 每 x 分钟保存一次
【发布时间】:2018-03-07 06:28:02
【问题描述】:

我想从一个 div 制作一个徽章,就像一个游戏徽章,我通过点击一个按钮将 html2canvas 保存到我的服务器,我想要它,所以我不需要每次都按下按钮图片更新了。

我使用的脚本是

function capture() {
    $('#box').html2canvas({
        onrendered: function (canvas) {
            $('#img_val').val(canvas.toDataURL("image/png"));
            document.getElementById("myForm").submit();
        }
    });
}

保存页面为:

    <?php
//Get the base-64 string from data
$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
//Decode the string
$unencodedData=base64_decode($filteredData);
//Save the image
file_put_contents('img.png', $unencodedData);
?>
<h2>Save the image and show to user</h2>
<table>
    <tr>
        <td>
            <a href="img.png" target="blank">
                Click Here to See The Image Saved to Server</a>
        </td>
        <td align="right">
            <a href="index.php">
                Click Here to Go Back</a>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <br />
            <br />
            <span>
                Here is Client-sided image:
            </span>
            <br />
<?php
//Show the image
echo '<img src="'.$_POST['img_val'].'" />';
?>

那么有没有办法每隔 x 分钟将 div 保存到图像?

【问题讨论】:

  • 查找settimout()

标签: javascript php jquery html2canvas


【解决方案1】:

您可以将捕获函数传递给 setInterval 函数,该函数接收以毫秒为单位的间隔作为第二个参数。

function capture() {
  $('#box').html2canvas({
    onrendered: function (canvas) {
        $('#img_val').val(canvas.toDataURL("image/png"));
        document.getElementById("myForm").submit();
    }
  });
}

setInterval(capture, 2000);

这将每 2 秒调用一次捕获函数。

【讨论】:

  • 那么在我的代码中我应该把它放在哪里,这样我就不需要做任何事情了?无需我的输入等即可运行。
  • 我已经编辑了我的答案,所以它显示了你可以把它放在哪里。
  • 那行得通,但你必须在页面上,有没有不需要在页面上,所以它在没有我的情况下运行?
  • 如果您不在页面上,您将如何捕获 $('#img_val')?我想不,这不是唯一的原因,抱歉。
  • 这就是为什么我需要第二个计划但使用 html2canvas,必须离开对吧?我希望...
猜你喜欢
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多