|
My best guess is that event.loaded and event.total are integers, not floating point numbers as you expect, so it's performing integer math.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Except that JavaScript doesn't really have a concept of "integers", unless you use the BigInt class.
Number : used for all number values (integer and floating point) except for very big integers.
BigInt : used for arbitrarily large integers.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Red Kipling wrote: const percentComplete = (event.loaded / event.total) * 100;
You might try the following. Mathematically it is the same but the order of operations changes how it computes the result.
const percentComplete = ((event.loaded * 100) / event.total);
|
|
|
|
|
Red Kipling wrote: upload a files in my website
I have this javascript code integrated to show the progress of upload
But the issue is that this bar has value of 100% before the upload is completed
How can i resolve this See what happens if you replace this code:
xhr.upload.onprogress = function (event) {
}; ...with this
xhr.upload.addEventListener("progress", function (event) {
}); Although your code is correct, try using the addEventListener method to see if that makes any difference. I believe that event listeners need to be assigned before the xhr's "open" method is called.
|
|
|
|
|
I have to add progressbar to kleeja upload script
I have a three inputs wich contain the files to upload
this is the css code for progressbar
#progress-bar {
width: 100%;
background-color: #f3f3f3;
}
#progress {
width: 0%;
height: 30px;
background-color: #4caf50;
text-align: center;
line-height: 30px;
color: white;
}
and this is the javascript code
function uploadFile() {
const fileInput = document.getElementById('file1');
const fileInput2 = document.getElementById('file2');
const file = fileInput.files[0];
const file2 = fileInput2.files[1];
const xhr = new XMLHttpRequest();
xhr.open('POST', '/index.php', true);
xhr.upload.onprogress = function (event) {
if (event.lengthComputable) {
const percentComplete = (event.loaded / event.total) * 100;
const progress = document.getElementById('progress');
progress.style.width = percentComplete + '%';
progress.textContent = Math.round(percentComplete) + '%';
}
};
xhr.onload = function () {
if (xhr.status === 200) {
alert('File uploaded successfully.');
} else {
alert('Error uploading file.');
}
};
const formData = new FormData();
formData.append('file1,file2', file);
xhr.send(formData);
}
i save this file in folder 'progressbar'
this is the form to submit for uploading
<pre> <form id="uploader" action="<?=$this->vars['action']?>" method="post" enctype="multipart/form-data">
<div class="card text-center">
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#fileUpload" role="tab"><?=$this->vars['lang']['DOWNLOAD_F'] ?? '{lang.DOWNLOAD_F}'?></a>
</li>
</ul>
</div>
<div class="card-body tab-content">
<?php if($this->vars['config']['safe_code']){ ?>
<!--
<div class="safe_code card">
<div class="card-body">
<h4 class="card-title"><?=$this->vars['lang']['VERTY_CODE'] ?? '{lang.VERTY_CODE}'?></h4>
<h6 class="card-subtitle mb-2 text-muted"><?=$this->vars['lang']['NOTE_CODE'] ?? '{lang.NOTE_CODE}'?></h6>
<p class="card-text">
<img style="vertical-align:middle;" id="kleeja_img_captcha" src="<?=$this->vars['captcha_file_path']?>" alt="<?=$this->vars['lang']['REFRESH_CAPTCHA'] ?? '{lang.REFRESH_CAPTCHA}'?>" title="<?=$this->vars['lang']['REFRESH_CAPTCHA'] ?? '{lang.REFRESH_CAPTCHA}'?>" onclick="javascript:update_kleeja_captcha('<?=$this->vars['captcha_file_path']?>', 'kleeja_code_answer');" />
<input type="text" name="kleeja_code_answer" id="kleeja_code_answer" />
</p>
</div>
</div>
<?php } ?>
<!--
<div class="tab-pane active" id="fileUpload" role="tabpanel">
<?php foreach($this->vars["FILES_NUM_LOOP"] as $key=>$value){ ?>
<div class="input-group mb-2" style="" id="file-block-<?=$value['i']?>" onclick="document.getElementById('file<?=$value['i']?>').click();">
<div class="btn btn-primary file-button-browse">
<img src="<?=$this->vars['STYLE_PATH']?>images/folder.png" width="20" height="20">
<?=$this->vars['lang']['OPEN'] ?? '{lang.OPEN}'?>
</div>
<input type="file" class="example" name="file_<?=$value['i']?>_" id="file<?=$value['i']?>" data-number="<?=$value['i']?>" style="display:none" >
<input type="text" id="file-text<?=$value['i']?>" disabled="disabled" placeholder="<?=$this->vars['lang']['NO_FILE_SELECTED'] ?? '{lang.NO_FILE_SELECTED}'?>" class="form-control">
</div>
<?php
} ?>
<div>
<br>
<div class="agree text-muted"><small><?=$this->vars['terms_msg']?></small></div>
<input type="submit" id="submitr" onclick="uploadFile()" name="submitr" value="<?=$this->vars['lang']['DOWNLOAD_F'] ?? '{lang.DOWNLOAD_F}'?>" class="btn btn-outline-primary">
</div>
</div>
</div>
</div>
<script src="progressbar/progress.js"></script>
</form>
how can i modify the codes to make the progressbar show the three files upload progress
Thanks a lot
|
|
|
|
|
It is taking around 3-4 seconds to load 500 records which is very slow as it is hampering my app performance.
var source = { datatype: "json",
datafields: [ { name: "Rid", type: "int" },
{ name: "ParentRid", type: "int" },
{ name: "Name", type: "string" },
{ name: "Text", type: "string" } ],
id : "Rid",
localdata : dataSource,
async: true,
hasThreeStates: false };
var dataAdapter = new $.jqx.dataAdapter(source);
dataAdapter.dataBind();
var records = dataAdapter.getRecordsHierarchy("Rid", "ParentRid", "items", [{ name: "Text", map: "label" }, { name: "Rid", map: "value" }, { name: "Name", map: "Name" }]);
$("#jqxtree").jqxTree({ checkboxes: true, source: records, width: '100%', height: 220 });
I want to load 'JqxTree' control in such a way that other things won't wait for it to load. Is there any way in the jQuery to load 'JqxTree' control in the background?
Or something we can do which won't affect performance of whole application
|
|
|
|
|
The obvious question is why are you trying to load 500 records into a tree? Are your users really going to dig through that many tree nodes to find the one they want? Surely there must be a better way to help them find what they're looking for?
Beyond that, you need to profile your code to find out where the bottleneck is. If it really is the call to the jqxTree method, then the problem is likely the browser struggling to handle DOM updates, and there will be nothing you can do to improve that. You would need support from the authors of the library, which means waiting for them to reply to your GitHub issue[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: why are you trying to load 500 records into a tree?
Good question.
I have seen others try to load 3,000 and even 300,000. First one is when I learned years ago as a backend developer to never write queries that do not have a paging limit. At least that way if the front end tries to load everything it won't blow up my code.
|
|
|
|
|
Thank you for the information.
|
|
|
|
|
|
Why not run it for yourself and find out?
Since none of the cases match 0 , none of the return statements will execute. The return value of the function will be undefined.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
C++, Java and now Javscript. Maybe you should focus on one language and learn it completely before moving to the next one. Especially as the code in the above question would behave exactly the same (including doing nothing for zero) in all three.
|
|
|
|
|
|
That code does not output what you/she says it does.
The first console.log statement will output 'undefined'.
The second will output 'One digit: ${x}'.
The third will output 'Two digit: ${x}'.
and the last will output 'The number is: ${x}'.
The 'undefined' is output because none of the cases in the switch block met any criteria so it fell through to the last (and non-existent) statement in the numberDigits function. There is no return statement there either, so the function ends without returning anything. Javascript will set the 'return value' to undefined, or null in other languages. Other languages will just crash or not even compile, but since javascript is interpreted, it can get away with an undefined return value.
|
|
|
|
|
Yes, all output is correct according to the code as written. But you can tell her to try values such as -23 and 1111.
|
|
|
|
|
Are you saying that passing 0 doesn't return undefined?
Thanks.
|
|
|
|
|
Clearly not as the test results show. Look at the last case statement:
case x > 0 || x <= 100:
That means if x is greater than zero OR if x is less than or equal to 100! So any postive number (which is greater than zero) OR any number less than 101 (which includes negative numbers). I suspect that is not what the code is supposed to do.
|
|
|
|
|
I screwed that up in my test code. You are correct.
|
|
|
|
|
You'd be better off putting just a little effort into your question. Not everyone wants to click an external link for an image of code for instance. People are usually happy to help as long as they aren't being used.
That being said, as mentioned, you and the person you're helping should learn to run code and test it yourself. If that's not something you're willing to do, then neither of you two need to be programming. This isn't being mean man; this is saying the industry doesn't need another coder who won't even run own code they're testing. At least run it with passing in zero and then asking why a return value is what it is.
Jeremy Falcon
|
|
|
|
|
I have a project that is to involve opening several image files onto the same place on the screen to overlay or not overlay them. I know nothing about java script, but someone gave me a sample script that sort of does what I'm trying to do.
In that script the files are all located on the web and it references the html links for them. I'd like to be easily shareable and not really have to rely on the images being hosted somewhere. I thought I could include the image files and along with the script all in one folder so they't all be installed in the same place. I tried the full path to that place and it doesn't seem to work.
I wish I knew more terminology, etc but like I said I really have know knowledge of javascript.
So that is the first part of my project.
I see that I can specify where on the screen to place the object and size.
The second part will be to add controls for pan and zoom for the section of the screen where the images will be displayed.
But, for now I simply want to see how to display a local file.
Thanks.
|
|
|
|
|
Javascript code is executed by the browser, in the user's system, so it does not have direct access to any file system. It can be used to load and save files on the user's system, but only under the user's control. So you have two choices: 1. locate the files on some public website as you are currently doig, or 2. Host them on the web server and modify the backend code to feed them to the web page. If you want to learn Javascript (with lots of good examples) go to JavaScript Tutorial[^].
|
|
|
|
|
Thanks to the link to the javascript tutorial. It looks like there is a lot of good info there. I just happened to come across that site if not the page yesterday.
I guess it makes sense that if java script is a language for web pages, access to local files would not be a good thing because of malware, etc.
I was playing around a little with putting a sample image online and putting the URL to it in the code I have. A complicating factor is that the images I want to use are SVGs. Most free hosting sites don't allow them as they are text files and can be full of code.
I did find where I could do this for free on GIT Hub or actually now it is a clone called GIT Hack. I just posted a different SVG than I had in mind. I wound up with a type mismatch error between the line I inserted it in and the last line. I still haven't sorted that out, but in looking into types and type mismatches is where I came across the site that the java script tutorial is on.
I also came across something called open layers which is a set of java script tools for working with layers which sounds like something I could use.
That said, I really would prefer though to create shareable file that includes the images and code, etc (not a link) so it is something that stands alone rather than online. I'd like to create something permanent. Free online things come and go, cease to be free, just change, etc. I don't want to have to pay perpetually either, in fact I won't be around forever either. One of the images I ultimately want to overlay is from the 1860s onto a more current Google Earth type map.
I think I can use photoshop pro to do something like this, but I think it costs a lot, think maybe anyone trying to view it might need it as well, etc.
So I guess I'll continue with java script for now.
Thanks again.
|
|
|
|
|
I think you should rethink what your trying to accomplish. Perhaps choose another path ...
I read your post and it makes little sense to me, where you used the word "Screen" instead of "browser" or "window".
JavaScript manipulates the DOM, which is a collection of HTML elements in a browsers view or tab. Most of the time, you create a web page using HTML, and if needed, you can write some JavaScript to move an element, change the word or color of a element. Or listen for events to occur, and fire a function when that event occurs. You can make web pages from 100% JavaScript, but it's time consuming, unless your app requires that level of sophistication. In other words it's really expensive to choose this method.
Consider writing a Windows app, or Mac app, you didn't say what platform you work with. Or take a lesson in HTML and how to create web pages first. I think your putting the cart in front of the horse here.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I said screen but technically it would be browser which of course you look at on a screen.
I do have an example of something similar. I have played around a with some simple webpages in the distant past, but for the most part just used a WYSIYG editor.
I mainly run linux, but can do Windows.
I may reconsider using photoshop as an option. Maybe I can get access to the app somewhere, somehow. I think I've read photoshop pro can use georeferenced pdfs as layers which would line things up and should have the other features for panning and zooming, etc. that I want.
I'm basically trying to set it up like a GIS set of layers only I want to be able to share it with people without GIS or anything special. The old image is a perspective drawing of a town that I have warped to fit pretty well with a current hybrid map of the area.
It is a little cluttered and hard to read with all the layers turned on. However if there is a specific spot you are interested you can pan/zoom to it and then switch more layers on. Sometimes altering the transparency and other adjustments can help too.
For now, I will sort of pursue the java script method a little more as I look into photoshop.
I looked into this about a year ago and those were the only two options I found then. I looked a little more recently and see a number of places to make overlaid images, etc but they were all for online use. That would work, but the images need to be hosted, etc. I'd rather just make something that works, you may need to copy a whole folder somewhere in your computer to use it, but you wouldn't even need a connection to view it.
|
|
|
|
|
That sounds more like a game to me. Or gaming technology, I think you can layer things, I'm not a gaming programmer so I don't know, but I think it's possible. (I haven't gamed in decades now). You should be able to program hidden buttons that would change the picture like a transition of scene.
I did some quick research, and they have JavaScript Libraries that allow you to create Game like or 3D stuff. Babylon must be open source, could be a start.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|