如何在jQuery中执行此操作?

时间:2017-04-22 00:38:37

标签: javascript jquery

我想将这段代码从JavaScript转换为jQuery。

这是当前的代码:

def ftp(self):  # Screen Grab and FTP Transfer

    new = ImageGrab.grab(bbox=(0, 50, 1366, 720))
    new = new.resize((1366, 700), PIL.Image.ANTIALIAS)
    new.save("C:\\Users\\Owner\\Desktop\\screenshots\\capture.jpg")

    newOpen = PIL.Image.open("C:\\Users\\user\\Desktop\\screenshots\\capture.jpg")
    newOpen.save("C:\\Users\\Owner\\Desktop\\screenshots\\capture.jpg", format="JPEG", quality=40)

    tries = 10  # Denotes maximum try limit for retry attempts

    for i in range(tries):
        try:

            # FTP image to Web Server
            session = ftplib.FTP('server', 'user', 'pass')
            file = open('C:\\Users\\Owner\\Desktop\\screenshots\\capture.jpg', 'rb')  # file to send
            session.storbinary('STOR capture.jpg', file)  # send the file
            file.close()  # close file and FTP
            session.quit()

            value = "Updated. \nFailed " + str(i) + " Times\n" + str(self.tick)

            print value

            self.tick += 1

        except KeyError as e:

            if i < tries - 1:  # i is zero indexed

                continue

            else:

                raise

        break

    threading.Timer(5, self.ftp).start()

这是我的尝试:

var i = 0;
    var start = true;

    document.getElementById("startclick").addEventListener("click", function() {
        if (start) {
            start = false;
            interval = setInterval(function() {
                i++;
                document.getElementById('list').innerHTML += "albastru_" + i + '<br>' ;
            }, 3000);
        } else {
            start = true;
            clearInterval(interval);
        }

    });

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作。我假设startclick是一个按钮,点击它后.append()会在3秒后将所需数据添加到#list div。

使用开始和停止按钮更新了答案。

var i = 0;
var start = true;
var interval = null;

$('#startclick').click(function() {
  if (interval !== null) return;
  interval = setInterval(function() {
      i++;
      $('#list').append("albastru_" + i + '<br>');
  }, 3000);  
});

$("#stop").click(function() {
  clearInterval(interval);
  interval = null;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<div id="list">
</div>
<br />
<button id="startclick">START!</button>
<button id="stop">STOP!</button>