etsted Posted March 29, 2014 Share Posted March 29, 2014 i am trying a script that wont work when i use double quotes like this: setTimeout('resetScroller("+el+")',speed); but instead i have to use thi syntax: setTimeout('resetScroller(''+el+'')',speed); whats the diffrence? Because they wont give the same result. Link to comment Share on other sites More sharing options...
Ingolme Posted March 29, 2014 Share Posted March 29, 2014 In your first example "el" is being interpretted as part of the string because you haven't closed the string: In the second example you have quotation marks inside the string but you're closing the string before showing the variable. It doesn't matter whether they're single or double quotes. All the following options are valid: "resetScroller('" + el + "')""resetScroller("" + el + "")"'resetScroller('' + el + '')''resetScroller("' + el + '")' The backslash is used to put a quotation mark inside a string that's delimited by the same quotation mark. Link to comment Share on other sites More sharing options...
etsted Posted March 29, 2014 Author Share Posted March 29, 2014 delimited? Link to comment Share on other sites More sharing options...
Ingolme Posted March 29, 2014 Share Posted March 29, 2014 The verb delimit: To fix or mark the limits or boundaries of; demarcate: A string is delimited by either single-quotes (apostrophe character) or double-quotes. There is one delimiter preceding the string, and another delimiter following the string. If you want to use a single-quote inside a string that is delimited by single-quotes you need to use a backslash. Look at how the syntax highlighter reacts to various strings based on what characters they contain: var str;str = "It's called a "delimiter".";str = 'It's called a "delimiter".'; //'str = "It's called a "delimiter".";str = 'It's called a "delimiter".'; Green means that the character is part of the string, black means it's outside of the string. Link to comment Share on other sites More sharing options...
davej Posted March 29, 2014 Share Posted March 29, 2014 What is the best approach? I don't seem to do that too often in JS. Where would I need to do that? <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>passing funct args</title><script>var p = {};p.cnt = 0;p.delay = 1000;p.multi = 10;window.onload = init;function init() {getele(1).innerHTML = 'Initialized to a delay of '+ mult(p.delay,p.multi) +' milliseconds<br/>';setTimeout(function(){displaycnt(getele(1),getpcount());},mult(p.delay,p.multi));}function getpcount(){p.cnt = p.cnt+1;return p.cnt;}function getele(n){var str = 'out'+ n;return document.getElementById(str);}function mult(x,y){return x*y;}function displaycnt(ele,cnt){var d = new Date();ele.innerHTML += 'Count: '+ cnt +' at time: '+ d.toLocaleTimeString() +'<br/>';setTimeout(function(){displaycnt(ele,getpcount());},mult(p.delay,p.multi));}</script></head><body><h1>FUNCTION ARGUMENTS ARE SCREWY AS ######</h1><div id="out1"></div></body> </html> Link to comment Share on other sites More sharing options...
Ingolme Posted March 29, 2014 Share Posted March 29, 2014 I see a lot of useless things in your code. It could be rewritten without any parameters and without the need for some of the functions. Link to comment Share on other sites More sharing options...
davej Posted March 29, 2014 Share Posted March 29, 2014 It was intended to be a silly example that passes a lot of functions as arguments -- which was (my interpretation) the OP's core problem. When exactly do you need to do what the OP is trying to do? Does that even work? setTimeout('resetScroller('+el+')',speed); ...doesn't this work better? setTimeout(function(){resetScroller(el)},speed); Link to comment Share on other sites More sharing options...
thescientist Posted April 10, 2014 Share Posted April 10, 2014 yes, because the best practice way to use setTimeout / Interval is by passing a reference to a function Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now