简体   繁体   English

如何在调用函数或执行其他操作之前更改背景颜色

[英]How to change background color before calling a function or doing something else

I want to change the color of the "lol" button before calling testFunction() . 我想在调用testFunction()之前更改“lol”按钮的颜色。

 function testFunction() { for (var i = 0; i < 200; i++) { console.log(i); } return 0; } $("button").click(function() { $("button").css("background-color", "#6ddc5d"); testFunction(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>lol</button> 

And How Can I do the same, without a function? 如果没有功能,我怎么能这样做呢? Example code below: 示例代码如下:

 $("button").click(function() { $("button").css("background-color", "#6ddc5d"); // change color, then run this below operation for (var i = 0; i < 200; i++) console.log(i); // more stuff here }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>lol</button> 

The issue is because the loop you're running is synchronous. 问题是因为您正在运行的循环是同步的。 This blocks the UI thread from updating the background colour you amend. 这会阻止UI线程更新您修改的背景颜色。

To solve this, call the testFunction() within a timeout with 0 delay: 要解决此问题,请在超时时调用testFunction() ,延迟为0

 function testFunction() { for (var i = 0; i < 200; i++) { console.log(i); } return 0; } $("button").click(function() { $("button").css("background-color", "#6ddc5d"); setTimeout(testFunction, 0); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>lol</button> 

The logic for your version without the function is the same, you just need to wrap it in a setInterval() : 没有该函数的版本的逻辑是相同的,你只需要将它包装在setInterval()

 $("button").click(function() { $("button").css("background-color", "#6ddc5d"); setTimeout(function() { for (var i = 0; i < 200; i++) { console.log(i); } }, 0); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>lol</button> 

You could use a worker if you didn't want to use a timeout. 如果您不想使用超时,则可以使用工作程序。 This will allow you to run heavy long running loads that take longer than the timeout. 这将允许您运行耗时超过超时的繁重的长时间运行负载。

Note 1: This works in all browsers, but seems to work the best in FireFox. 注1:这适用于所有浏览器,但似乎在FireFox中效果最好。
Note 2: Edge complains about the inline script when using createObjectURL , so an external script would be better. 注意2: Edge在使用createObjectURL时会抱怨内联脚本,因此外部脚本会更好。

 // Get the worker form an inline script var blob = new Blob([document.querySelector('#worker1').textContent ], { type: "text/javascript" }) // Get the URL to the worker (can use an external file) var worker = new Worker(window.URL.createObjectURL(blob)) // Listen for messages from the worker // When when get one handle it worker.onmessage = function(e) { console.log(e.data) } // Once clicked change the color, then send a message to the worker $("button").click(function() { $("button").css("background-color", "#6ddc5d") worker.postMessage('') }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>lol</button> <!-- Place the contents of the below script in an external js file if desired --> <script id="worker1" type="javascript/worker"> // Listen for messages from the main process self.onmessage = function(e) { for (var i = 0; i < 2000; i++) { // Send messages back to the main process self.postMessage(i) } return 0; } </script> 

The above example uses an inline worker, you could replace it with an external .js file if you wanted, you would just need to remove the var blob = ... line and replace window.URL.createObjectURL(blob) with the URL to the file. 上面的示例使用内联工作程序,您可以根据需要将其替换为外部.js文件,您只需要删除var blob = ...行并将window.URL.createObjectURL(blob)替换为URL文件。

I guess you want it to be executed seperately. 我猜你想要它分开执行。 In this case you need to make it async with let's say setTimeout. 在这种情况下,您需要使用setTimeout将其设为异步。

 function testFunction() { for (var i = 0; i < 200; i++) { console.log(i); } return 0; } $("button").click(function() { $("button").css("background-color", "#6ddc5d"); setTimeout(function() { testFunction(); },0); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>lol</button> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在执行其他任何操作之前,先测试一个函数是否正在等待 - Test that a function awaits something before doing anything else 使用JQuery在执行其他操作之前检查属性是否存在 - With JQuery check if an attribute exists before doing something else 如何使用 jQuery 选择器和 if else 语句更改按钮背景颜色? - How to change the button background color using jQuery selectors and if else statement? Express:如何在调用某些函数之前等待函数完成 - Express: How to wait for a function to complete before calling something 我正在执行切换,并且当切换功能启动时,我想更改div的背景颜色更改 - I am doing a toggle and I want to change the background color change of the div when the toggle function fired 如何在FullCalendar中更改今天前几天的背景颜色? - How to change background color of days before today in FullCalendar…? 如何在 hover 上更改::before 的背景颜色? - How can i change background color of ::before on hover? 当我输入并提交内容时,如何停止输入表单以更改背景颜色和文本颜色? - How do I stop the input form to change background color and text color when I input and submit something? 当发生特定情况时,如何更改html表条目背景的背景颜色? - How do you change the background color of an html table entry background when something specific happens? 如何通过更改其他颜色来指示已单击某些内容? - How to indicate that that something has been clicked by changing color of something else?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM