如何在特定间隔后添加计时器以捕获屏幕截图

时间:2018-02-28 08:21:59

标签: javascript

这段代码是关于如何拍摄截图,我希望在这个像onclicked addlistener中添加计时器它应该在特定间隔后捕获屏幕截图。所以它会自动捕获屏幕。请仔细研究并帮助我。

var screenshot = {
    content : document.createElement("canvas"),
    data : '',

    init : function() {
        this.initEvents();
    },

    saveScreenshot : function() {
        var image = new Image();
        image.onload = function() {
            var canvas = screenshot.content;
            canvas.width = image.width;
            canvas.height = image.height;
            var context = canvas.getContext("2d");
            context.drawImage(image, 0, 0);

            // save the image
            var link = document.createElement('a');
            link.download = "download.png";
            link.href = screenshot.content.toDataURL();
            link.click();
            screenshot.data = '';
        };
        image.src = screenshot.data; 
    },

    initEvents : function() {
        chrome.browserAction.onClicked.addListener(function(tab) {
            chrome.tabs.captureVisibleTab(null, {
                format : "png",
                quality : 100
            }, function(data) {
                screenshot.data = data;

                // send an alert message to webpage
                chrome.tabs.query({
                    active : true,
                    currentWindow : true
                }, function(tabs) {
                    chrome.tabs.sendMessage(tabs[0].id, {ready : "ready"}, function(response) {
                        if (response.download === "download") {
                            screenshot.saveScreenshot();
                        }
                        else {
                            screenshot.data = '';
                        }
                    });
                }); 

            });
        });
    }
};

screenshot.init();

1 个答案:

答案 0 :(得分:0)

您应该查看SetInterval

例如:

function test(){
    alert('Doing Something here');
}

setInterval(test,2000);

所以将所有逻辑包装在一个函数中,并以每x毫秒的间隔调用它