Monday, June 3, 2024
185
rated 0 times [  189] [ 4]  / answers: 1 / hits: 22283  / 11 Years ago, fri, july 12, 2013, 12:00:00

I want to seperate thread on the page to prevent freezing of gui. For this, I am running the function which will freeze gui inside another thread with setTimeout but still freezing.



The code and jsbin link are below:



<!DOCTYPE html>
<html>
<head>
<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js
type=text/javascript></script>
<meta charset=utf-8 />
</head>
<body>
<div id=div1></div>
<div id=div2></div>
<input type=button value=düðme id=btn />

<script type=text/javascript>
$(#btn).on(click,function(){
$(#div1).html(new Date());
});

$(document).ready(function(){
setTimeout(function() { count(); },1);
});

function count(){
for(var i =0;i<100000;i++){
$(#div2).html(i);
}
$(#div2).append(new Date());
}
</script>

</body>
</html>

More From » multithreading

 Answers
3

Even though you have delegated execution via setTimeout it will still be executed in the same single thread, it will just wait for its time in queue and will postpone any other activity until it's done.



Please refer to this awesome picture from Secrets of JS Ninja book:



enter


[#77040] Thursday, July 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monetm

Total Points: 615
Total Questions: 103
Total Answers: 119

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
monetm questions
Fri, Feb 26, 21, 00:00, 3 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Sun, Jul 26, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;