Tampermonkey js / jquery / ajax如何添加睡眠?

时间:2017-01-05 02:23:53

标签: jquery ajax tampermonkey

对于像这样的页面 https://www.converto.io/?v=EbuYMynCWV8, 我有一个自动的Tampermonkey脚本:

  1. 选择mp4格式
  2. 点击“转换”按钮。
  3. 它有效,但有时候可能太快了,最后我得到了mp3格式的下载链接 所以现在我想在两个步骤之间进入休眠时间。测试结果是代码只完成第一步。有什么想法吗?

    我的代码:

    // ==UserScript==
    // @name     _ConverTo, Automatically select mp4
    // @match    https://www.converto.io/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
    // @grant    GM_addStyle
    // ==/UserScript==
    //- The @grant directive is needed to restore the proper sandbox.
    
    waitForKeyElements (".format-select:has(option[value=mp4])", selectFinickyDropdown);  //------ step 1,choose mp4 format------
    
    function selectFinickyDropdown (jNode) {
        var evt = new Event ("click");
        jNode[0].dispatchEvent (evt);
    
        jNode.val('mp4');
    
        evt = new Event ("change");
        jNode[0].dispatchEvent (evt);
    
        setTimeout("secondStep()", 10000);    //--- sleep 10s then step 2,click CONVERT button -------
    }
    
    function secondStep() {
        waitForKeyElements (".convert-btn", clickbuttonconvert);
    }
    
    function clickbuttonconvert (jNode) {
        var evt = new Event ("click");
        jNode[0].dispatchEvent (evt);
    }
    

1 个答案:

答案 0 :(得分:1)

setTimeout调用中删除引号和parens,第一个参数应该是函数本身。

setTimeout(secondStep, 10000); 

the setTimeout reference at MDN