在确认对话框弹出之前做一些事情

时间:2018-11-24 20:48:51

标签: javascript jquery confirm

$('.lorem').on('click', function(){
    $(this).hide();
    if(prompt('DO SOMETHING') != null) {console.log('something');}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='lorem'>lorem</div>

因此,我首先要隐藏div,然后弹出确认对话框。有可能吗?

2 个答案:

答案 0 :(得分:1)

使用动画帧在下一次绘制之前运行代码。

window.requestAnimationFrame()

static void ProcessDirectory(string writeTo, string inPath)
    {
        var Sort = Directory.EnumerateFiles(inPath, "*", SearchOption.AllDirectories)
                            .OrderByDescending(f => new FileInfo(f).Length);

        long b = 0;
        TextWriter tw = new StreamWriter(writeTo, true);

        foreach (string name in Sort)
        {

            FileInfo info = new FileInfo(name);
            long len = info.Length;
            tw.WriteLine(name + ": " + len);
            b += info.Length;
            }


        tw.WriteLine(b);
        tw.Close();
        // 4.
        // Return total size
    }

    static void Main(string[] args)
    {
        string outPath = @"/Users/a/Documents/size.txt";
        TextWriter tw = new StreamWriter(outPath, true); //This must come first to create the file

        ProcessDirectory(outPath, @"/Users/a/Downloads");
        tw.Close();
    }

注意:由于浏览器倾向于以不同的方式实现请求动画帧,因此您可能必须嵌套动画帧。

$('.lorem').on('click', function(){
    // The browser will paint async not sync, so the div may still be visible
    // even after this line
    $(this).hide();
    // when the browser is ready to paint the div off screen the callback will fire
    window.requestAnimationFrame(() => { 
        if (prompt('DO SOMETHING') != null) {
            console.log('something');
        }
    });
});

答案 1 :(得分:0)

您可以使用setTimeout:

 document.querySelector('.lorem').addEventListener('click',  () => {
  document.querySelector('.lorem').style.display = "none";
   setTimeout(() => {
     if(prompt("do something") !== null) {
       console.log('do something')
     }
   }, 100)
  })